Add this line of code to a PHP script
var_dump(curl_version());
If there was a error similar to this Fatal error: Call to undefined function curl_version() or no results was shown, it means that CURL is not installed.
var_dump(curl_version());
$ar = array("First" => 1, "Second" , "Third" =>"Three");
foreach($ar as $key => $val) {
var_dump($key);
}
string 'First' (length=5)
int 0
string 'Third' (length=5)
array( 0 => array(
"First" => 1, "Two", "Third" => "Three"
)
);
foreach($ar as $k => $v){
var_dump($ar[0][$k]);
}
string 'Two' (length=3)
string 'First' (length=5)
int 0
string 'Third' (length=5)
array( 0 => array(
"First" => 1, "Two", "Third" => "Three"
)
);
foreach ($ar as $k => $v) {
foreach ($v as $k2 => $v2) {
var_dump($k2);
}
}
string(5) "First"
int(0)
string(5) "Third"
"id","dealer_id","vin","stockno",
"1","2","3","4",
"5","6","7","8",
"9","10","11",12"
"id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
"id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
"id"=>"5", "dealer_id"=>"6", "vin"=>"7", "stockno"=>"8"
"id"=>"9", "dealer_id"=>"10", "vin"=>"11", "stockno"=>"12"
<?php
$file = fopen('data.txt', 'r');
$headers = fgetcsv($file);
$result = array();
while ($row = fgetcsv($file)) {
if (!$row[0]) continue;
$nextItem = array();
for ($i = 0; $i < 4; ++$i) {
$nextItem[$headers[$i]] = $row[$i];
}
$result[] = $nextItem;
}
fclose($file);
var_dump($result);
fgetcsv to read each delimited row of the file. The first row is used as the header names. Then, for each other row, we create a new entry and match the indexes to the header names. I set it to 4 because you have a trailing delimiter that creates an empty entry at the end, if you removed those from the input you could use the length of $headers for the inner loop.var_dump for your data:array(3) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["dealer_id"]=>
string(1) "2"
["vin"]=>
string(1) "3"
["stockno"]=>
string(1) "4"
}
[1]=>
array(4) {
["id"]=>
string(1) "5"
["dealer_id"]=>
string(1) "6"
["vin"]=>
string(1) "7"
["stockno"]=>
string(1) "8"
}
[2]=>
array(4) {
["id"]=>
string(1) "9"
["dealer_id"]=>
string(2) "10"
["vin"]=>
string(2) "11"
["stockno"]=>
string(3) "12""
}
}
array1 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
);
array2 is array(
"0"=>"field3",
"1"=>"field2",
"2"=>"field0",
"3"=>"field1",
"4"=>"field6",
"5"=>"field5",
);
array2 is array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
"4"=>"field6",
"5"=>"field5",
);
<?php
$array1 = array(
"0"=>"field0",
"1"=>"field1",
"2"=>"field2",
"3"=>"field3",
);
$array2 = array(
"0"=>"field3",
"1"=>"field2",
"2"=>"field0",
"3"=>"field1",
"4"=>"field6",
"5"=>"field5",
);
$array3 = array_merge(
array_intersect($array1, $array2),
array_diff($array2, $array1)
);
var_dump($array3);
$array3 = array_map('strtolower', $array1);
$array4 = array_map('strtolower', $array2);
$array5 = array_merge(
array_intersect($array3, $array4),
array_diff($array4, $array3)
);
var_dump($array5);
$anArray = array("X", "Y", "Z");
unset($anArray[0]);
//'dumps' the content of $anArray to the page:
var_dump($anArray);
The output of the var_dump function will be:
array(2) { [1]=> string(1) "Y" [2]=> string(1) "Z" }
$anArray = array("V", "W", "X", "Y", "Z");
unset($anArray[2]);
//'dumps' the content of $anArray to the page:
var_dump($anArray);
This would output the following:
array(4) { [0]=> string(1) "V" [1]=> string(1) "W"
[3]=> string(1) "Y" [4]=> string(1) "Z" }
Note in the output above, there is now no index # 2 – there is 0, 1, 3,
and 4. Not having the continuous index values could potentially be a
negative drawback. So what are you alternatives? Well, you could use a
function called array_splice instead.
$anArray = array("V", "W", "X", "Y", "Z");
/*The 2 represents the offset - which basically
means move 2 positions from the beginning of the
array and that will take us to the "X" element. The 1
represents the length of the array that you want to
delete. Since we just want to delete 1 element, we
set the length parameter to 1.
And, since we are not replacing that element with anything
- we do just want to delete it - we leave the 4th parameter (
which is optional) blank
*/
array_splice($anArray, 2, 1);
var_dump($anArray);
Running the code above will return us this:
array(4) { [0]=> string(1) "V" [1]=> string(1) "W"
[2]=> string(1) "Y" [3]=> string(1) "Z" }
So, using array_splice will set the index values back to their correct
order, and we will be good to go again. There is however an assumption
here that we should point out – the array_splice function accepts a
value for the offset, not the index. Theoffset that we used in
the example above happened to be equal to the index value. This is the
case when the array we are dealing with already has a continuous integer
index value, but if the array has been changed for whatever reason
before array_splice is used, that may not always be the case.
$anArray = array("V", "W", "X", "Y", "Z");
/*
This will cause index 2 to go missing,
so the array indices of $anArray will be
0,1,3,4 - obviously the 2 is missing
*/
unset($anArray[2]);
/*
array_values will take an array as an input
and then take the array values (not the keys,
but just the values), and numerically index those values
into a new array.
This means that array_values will essentially re-index
the array that is given as an input, which restores the indices
to the correct order of 0,1,2, and 3:
*/
$anArray = array_values($anArray);
//'dumps' the content of $anArray to the page:
var_dump($anArray);
This will now output:
array(4) { [0]=> string(1) "V" [1]=> string(1) "W"
[2]=> string(1) "Y" [3]=> string(1) "Z" }
$key = array_search($valueToSearch,$arrayToSearch);
if($key!==false){
unset($array[$key]);
}
Hello Friends! I am Ramana a part time blogger from Hyderabad.