I'm attempting to build an array for CSV export and can't figure out why my array is building a sub array with just a string.
Any help would be appreciated.
PHP:
foreach($Entry as $list){
$pushArray = array($list['firstEntry'], $list['secondEntry']);
array_push($csv, $pushArray);
}
echo "<pre>";
print_r($csv);
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Title1
[1] => Title2
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 1
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 1.1
)
)
[2] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 2
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 2.1
)
)
[3] => Array
(
[0] => SimpleXMLElement Object
(
[0] => Test Entry 3
)
[1] => SimpleXMLElement Object
(
[0] => Test Entry 3.1
)
)
)
I want it to look like this:
Array
(
[0] => Array
(
[0] => Title1
[1] => Title2
)
[1] => Array
(
[0] => Test Entry 1
[1] => Test Entry 1.1
)
[2] => Array
(
[0] => Test Entry 2
[1] => Test Entry 2.1
)
[3] => Array
(
[0] => Test Entry 3
[1] => Test Entry 3.1
)
)
$pushArray = array((string) $list['firstEntry'], (string) $list['secondEntry']);
You've to cast SimpleXML elements to string type if you're using it that way
Related
UPDATED after trying the below suggestion it now gives the correct affect but enters it to many times.
Hi I have the following code what this does is creates two arrays then blend them into one:
$list_claimed_users = get_users('meta_key=claimed');
foreach ( $list_claimed_users as $list_claimed_user ) {
$a = get_user_meta($list_claimed_user->ID , "claimed");
$nicknames[] = get_user_meta($list_claimed_user->ID , "nickname");
$unserialized[] = unserialize($a[0]);
foreach($unserialized as $key => &$metadata) array_push($metadata, $nicknames[$key]);
}
$claimed_array[] = $unserialized;
print_r($claimed_array);
BUt it now loops and adds the $a data in an extra time for each loop
Array
(
[0] => Array
(
[0] => Array
(
[0] => 31.08.2016
[1] => prize8
[2] => Array
(
[0] => test5
)
[3] => Array
(
[0] => test5
)
[4] => Array
(
[0] => test5
)
[5] => Array
(
[0] => test5
)
)
[1] => Array
(
[0] => 31.07.2017
[1] => prize7
[2] => Array
(
[0] => test6
)
[3] => Array
(
[0] => test6
)
[4] => Array
(
[0] => test6
)
)
[2] => Array
(
[0] => 31.08.2017
[1] => prize5
[2] => Array
(
[0] => test7
)
[3] => Array
(
[0] => test7
)
)
[3] => Array
(
[0] => 21.08.2017
[1] => prize6
[2] => Array
(
[0] => test8
)
)
)
)
Could anyone please advise the best way to merge the two if if there is a simplier way to achieve this when creating the arrays originally.
Any help would be hugely appreciated
After retrieving the nicknames and the meta-data, but before adding result to $claimed_array, you can do:
foreach($a as $key => &$metadata) array_push($metadata, $nicknames[$key]);
Then just add $a to the $claimed_array:
$claimed_array[] = $a;
I have an xml source that brings me some data each time. I parse the xml with php's simpleXML and the generated array looks like the one below. My problem is that i need to sort the array ascending based on the value that carries the time.
Array
(
[0] => SimpleXMLElement Object
(
[dict] => Array
(
[0] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => OTE SPORT 3
[1] => ΕΡΓΟΤΕΛΗΣ - ΤΡΙΚΑΛΑ
[2] => 20:00
[3] => Ποδόσφαιρο
)
)
[1] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => NOVASPORTS 1
[1] => ΠΛΑΤΑΝΙΑΣ - ΟΛΥΜΠΙΑΚΟΣ
[2] => 19:30
)
)
)
)
[1] => SimpleXMLElement Object
(
[dict] => Array
(
[0] => SimpleXMLElement Object
(
[key] => Array
(
[0] => channel
[1] => title
[2] => time
)
[string] => Array
(
[0] => NOVASPORTS 2
[1] => ΚΟΛΟΣΣΟΣ - ΟΛΥΜΠΙΑΚΟΣ
[2] => 16:00
[3] => Μπάσκετ
)
)
)
)
)
Here is my problem how can i get the array[1]
i have an foreach on my view i can't get the value of [1] => Array?
Array
(
[0] => Array
(
[details] => Array
(
[product_id] => 1
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => メス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥98,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462124.jpg
[0] => 1394462124.jpg
)
)
)
[1] => Array
(
[details] => Array
(
[product_id] => 2
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => オス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥88,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462181.jpg
[0] => 1394462181.jpg
)
)
)
i will fetch the [1] => Array inside the
i can't get the [1] => Array using foreach on my view what is can i get the value on array[1]
If you just want to loop through and deal with each dataset, the following code will do that:
foreach ($arr as $key => $data) {
// Output data
// The first row will be $arr[0], and the second $arr[1], etc…
}
-- Original answer
Assuming you mean the second array within the top-level array and that you only want to access the second element in that array (index 1), you should just be able to access it as follows:
$arr[1]
eg:
$second_photo = $arr[1];
Where $arr is the variable name you've assigned to the array you've var_dump()ed above.
That said, your question is ambiguous and not very clear. Can you please provide more information as to what you are actually trying to achieve? (Loop though and display all instances, for example?)
I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/#attr|Parent/x
It grab the parent attr's value and all of its x's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first
I need to reorder an multidimensional array in php but I can't get it right and I am struggling with foreach, for and while loops for some time now.
This is what I have:
Array
(
[dimension] => Array
(
[dimension.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => geheel
)
[1] => SimpleXMLElement Object
(
[0] => geheel
)
)
[dimension.type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => hoogte
)
[1] => SimpleXMLElement Object
(
[0] => breedte
)
)
[dimension.value] => Array
(
[0] => SimpleXMLElement Object
(
[0] => 73
)
[1] => SimpleXMLElement Object
(
[0] => 84
)
)
[dimension.unit] => Array
(
[0] => SimpleXMLElement Object
(
[0] => cm
)
[1] => SimpleXMLElement Object
(
[0] => cm
)
)
)
[material] => Array
(
[material.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => deelmateriaal
)
[1] => SimpleXMLElement Object
(
[0] => deelmateriaal2
)
)
[material_type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => typemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => typemateriaal2
)
)
[material] => Array
(
[0] => SimpleXMLElement Object
(
[0] => materiaal
)
[1] => SimpleXMLElement Object
(
[0] => materiaal2
)
)
[material.notes] => Array
(
[0] => SimpleXMLElement Object
(
[0] => notemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => notemateriaal2
)
)
)
)
And I need to transform it to:
dimensions => Array ( 1 => array(dimension.part => geheel)
=> array (dimension.type => hoogte) )
.....
=> Array ( 2 => array(dimension.part => ...)
=> array (dimension.type => ...) )
....
....
material => Array ( 1 => ...
2 => ...
=> Array ( 1 =>
=>
....
Anyone got a good approach to this?
Thanks,
Joris
foreach($arr['dimension'] as $keyType => $type) {
foreach($type as $key => $value) {
$aNewArray[$key][$keyType] = $value;
}
}
Something like that.. And then process it a bit more to suit your needs..
What it does: walk through every dimension array (and store the key of it in $keyType) and then walk through every item and add it to a new array in the way you want it. It assumes every type has the same amount of items under it.
$new_array = new array();
$count = count($array['dimension']['dimension_part']);
for($i=0;$i<$count;$i++) {
$new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0];
$new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0];
$new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0];
}
I am not quite sure how you access simplexmlobjects, so I used the array type. maybe change it to [$i] -> 0