This question already has answers here:
Get value without knowing key in one-pair-associative-array
(4 answers)
Closed 5 years ago.
i have this scenario of having a mixed response from a server and i need to process its data in PHP
Array
(
[14424174] => Array
(
[0] => Array
(
[id] => 45
[nm] => This is a driver name
[ph] => 5454545
)
)
)
I want to access id, nm, ph values
but i had no luck cause this index number (14424174) is unknown to me, so i need to first store this index and then parse the array
Use a nested foreach():
foreach($arr as $i => $sub_arr)
{
foreach($sub_arr as $sub_i => $sub_sub_arr)
{
$id = $sub_sub_arr['id'];
$nm = $sub_sub_arr['nm'];
$ph = $sub_sub_arr['ph'];
}
}
You can use the following pattern:
foreach($array as $key=>$val) {
//get the id:
var_dump($key)//14424174
$nm = $val[nm];
$ph = $val[ph];
}
Related
This question already has answers here:
How to extract data from csv file in PHP
(13 answers)
Closed 1 year ago.
Apologies if this question is close to others. I have tried every solution to accomplish something simple with nothing but failure. :-(
I want the keys from array one to be assigned as keys to array two.
$demos_keys = array_keys($demos);
//$c = array_combine($demos_keys, $csvdata); produces "FALSE"
//$c = $demos_keys + $csvdata; simply adds the arrays but doesn't assign keys
So then I tried to loop through each element to assign the keys manually - to no avail!
foreach ($csvdata as $row){
for($i = 0; $i<count($demo_keys); $i++) {
$csvdata[$demo_keys[$i]]=$row[$i];
}
}
demos_keys:
lastname":"lastname","email":"email","d1":"phone","d2":"status"
csvdata:
"Dryer,fdryer#email.com,Backfield,North\r","Harris,fharris#email.com,Corp,South\r",etc.
I feel the csvdata array is wonky somehow. Every thing say it is an array with about 1000 rows, but the carriage return at the end of the last element is troubling me. I thought I'd deal with it later.
What else can I try!? Thank you all for any contributions!
It looks like each row of your CSV data has not been parsed into separate variables (are you reading it from a file using fgets or file instead of fgetcsv?). So you need to split it before you can combine it with the keys from $demos_keys. Something like this should work:
$demos_keys = array("lastname","email","d1","d2");
$csvdata = array("Dryer,fdryer#email.com,Backfield,North\r","Harris,fharris#email.com,Corp,South\r");
$result = array();
foreach ($csvdata as $row) {
$data = explode(',', trim($row));
$result[] = array_combine($demos_keys, $data);
}
print_r($result);
Output:
Array
(
[0] => Array
(
[lastname] => Dryer
[email] => fdryer#email.com
[d1] => Backfield
[d2] => North
)
[1] => Array
(
[lastname] => Harris
[email] => fharris#email.com
[d1] => Corp
[d2] => South
)
)
Demo on 3v4l.org
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have two types of arrays:
1:
$array1["a"][] = "value1";
$array1["a"][] = "value2";
$array1["b"][] = "value3";
2:
$array2["0"] = "a";
What I need now is to somehow find difference between these two arrays. I need to filter out array1 by key, which is located in array2 value. I have tried doing the following:
array_diff(array_keys($array1), array_values($array2));
But I get the following error on that line:
ErrorException Array to string conversion
Any ideas?
Something like this?
foreach ($array1 as $key => $value)
if( array_search ($key , $array2 ))
unset($array1[$key]);
If $array1 needs to have the values, you just need to put the diff in $array1 :
$array1 = array_diff(array_keys($array1), array_values($array2));
Depending on how you constructed your arrays, it should work. The following code (based on your question) worked:
<?php
$array1=array("a" => array(),"a" => array(),"b" => array());
$array2=array("0"=>"a");
print_r(array_keys($array1));
echo("<br/>");
print_r(array_values($array2));
echo("<br/>");
print_r(array_diff(array_keys($array1), array_values($array2)));
>
This results in:
Array ( [0] => a [1] => b )
Array ( [0] => a )
Array ( [1] => b )
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key
This question already has answers here:
PHP - Append elements to array
(3 answers)
Closed 8 years ago.
I'm trying to populate all the links in my database into an array with a key something like this:
$link = array(99999 => "http://link1.com", 111111 => "http://link2.com");
Which returns:
Array ( [99999] => http://link1.com [111111] => http://link2.com )
How can i get the array to look like that when going through a loop, Here is my code:
while($row = mysql_fetch_array($query)) {
$link[] = "$row[r_id] => $row[r_link]";
}
It returns:
Array ( [0] => 24004 => http://link1.com [1] => 30554 =>
http://link2.com
But i don't wan't it to be like this, How can i get it the same as the first example while going through a loop ?
while($row = mysql_fetch_array($query)) {
$link[$row['r_id']] = $row['r_link'];
}
$link[$row['r_id']] = $row['r_link'];