reorder multidimensional php array trouble - php

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

Related

How to get the name and values from xml array using php

Here is my Xml code:
SimpleXMLElement Object
(
[resultsRows] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => date
[value] => 20140102
[label] => Date
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => browserType
[value] => Chrome
[label] => Browser Type
)
)
)
[metric] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visitDuration
[value] => 1242
[label] => Avg. Visit Duration
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => bounces
[value] => 3
[label] => Bounces
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => repeatVisitors
[value] => 0
[label] => Repeat Visitors
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => newVisitors
[value] => 5
[label] => New Visitors
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visits
[value] => 10
[label] => Visits
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => pageViews
[value] => 66
[label] => Page Views
)
)
)
)
)
)
)
Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside #attributes nested array like key and value.
Thanks.
Found in the comments on php SimpleXml documentation:
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
Check the docs here: http://cl1.php.net/ref.simplexml

Codeigniter Unidentified Index

I have a function foo that returns an array with the below structure and what I am trying to work out is there a better way to structure my PHP as I am unsure about the indexing within the foreach as I want every ListingId
Goal:
I am wanting to write a foreach loop that gets the XML <Name> from an external XML document (Have not coded this section yet as I need to pass the ListingID from foointo the url to get the <Name>)
PHP:
$test0 = $this->foo();
$test = $test0[0]['ListingId'];
Structure:
Array ( [0] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712629 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [1] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712202 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [2] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532711566 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [3] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710864 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [4] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710271 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [5] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532691526 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [6] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 527496168 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3399- ) ) )
Try this way:
foreach($this->foo() as $foo) {
//here you can use your ListingId
var_dump($foo['ListingId']);
}

how do I merge these arrays?

I have this array structure:
Array
(
[0] => Array
(
[0] => Array
(
[timestamp] => 1310394569
[title] => SimpleXMLElement Object
(
[0] => A static tweet here from the static xml
)
[link] => SimpleXMLElement Object
(
[0] => http://www.google.com
)
[type] => SimpleXMLElement Object
(
[0] => static
)
)
)
[1] => Array
(
[0] => Array
(
[timestamp] => 1310117641
[title] => SimpleXMLElement Object
(
)
[link] => SimpleXMLElement Object
(
[0] => http://www.facebook.com/
)
[type] => SimpleXMLElement Object
(
[0] => facebook
)
)
[1] => Array
(
[timestamp] => 1309856547
[title] => SimpleXMLElement Object
(
)
[link] => SimpleXMLElement Object
(
[0] => http://www.facebook.com/
)
[type] => SimpleXMLElement Object
(
[0] => facebook
)
)
But I want to get rid of the outer containing array...so I'm left with this:
Array
(
[0] => Array
(
[timestamp] => 1310394569
[title] => SimpleXMLElement Object
(
[0] => A static tweet here from the static xml
)
[link] => SimpleXMLElement Object
(
[0] => http://www.google.com
)
[type] => SimpleXMLElement Object
(
[0] => static
)
)
[1] => Array
(
[timestamp] => 1310394569
[title] => SimpleXMLElement Object
(
[0] => A static tweet here from the static xml
)
[link] => SimpleXMLElement Object
(
[0] => http://www.google.com
)
[type] => SimpleXMLElement Object
(
[0] => static
)
)
array merge isn't doing anything at all....is there any way to do this?
Your example is a bit unclear, but assuming you want to turn this
array(
array(
array(…)
)
array(
array(…)
)
)
into
array(
array(…)
array(…)
)
this'll do:
$array = array_map('current', $array);
If you want to turn this
array(
array(
array(…)
)
array(
array(…)
array(…)
)
)
into
array(
array(…)
array(…)
array(…)
)
this should do:
$array = array_reduce($array, function ($result, $array) {
return array_merge($result, $array);
}, array());
For multidimensional arrays you have to use array_merge_recursive.

PHP search key in array and return its value?

I would like to search key in multidimensional array and i would like to get corrosponding value associated with that key.
For e.g.
I would like to extract following texts from below array :
SENT AT 12.08ms
And the text
sample id 41962
following is an array print_r() output :
Array
(
[0] => Array
(
[VERSION] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => 3.0
)
)
)
)
[SAMPLE] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample id 41962
)
)
)
)
[TSAM] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
)
[value] => Array
(
[0] => Array
(
[0] => sample group 141
)
[1] => Array
(
[0] => ¯
)
[2] => Array
(
[0] => sample batch 81
)
[3] => Array
(
[0] =>
)
[4] => Array
(
[0] =>
)
)
)
)
[STATUS] => Array
(
[0] => Array
(
[group] =>
[param] => Array
(
[TYPE] => Array
(
[0] => CART
)
)
[value] => Array
(
[0] => Array
(
[0] => SENT AT 12.08ms
)
)
)
)
)
)
Can somebody provide me optimized code for above problem. The multidimensional array contains more than 5000 to 10000 arrays.
Please, see if my function works for you:
function get_value_by_key($array,$key)
{
foreach($array as $k=>$each)
{
if($k==$key)
{
return $each;
}
if(is_array($each))
{
if($return = get_value_by_key($each,$key))
{
return $return;
}
}
}
}
Use:
$array = array('array1'=>array('array2'=>array('find_some_key'=>'some_value')));
echo get_value_by_key($array,'find_some_key'); // outputs: some_value
If all the array keys have the same structure the following code should work:
foreach($array as $item){
$sentat = $item['STATUS'][0]['value'][0][0];
$sample = $item['SAMPLE'][0]['value'][0][0];
}
More detailed information would help us to provide you more tips :)

Conditionally replace values in multidimensional PHP array

There has been a few other questions regarding replacing values in multidimensional array on here, but I didn't find anything regarding what I was trying to do, exactly, per se.
I have an array that I get from an API and I need to update a few values based on other values in the array tree before sending the API payload to the browser.
In the array when the sale[0] === true I am looking for some logic to then replace the [price][0] value with a corresponding new sale price.
Using the foreach, I can easily loop through the each of the nodes in the array, but I am unsure once I loop through the array, how I can update the original array with new price if/when sale node === true.
Array
(
[response] => Array
(
[0] => Array
(
[results] => Array
(
[0] => Array
(
[items] => Array
(
[0] => Array
(
[id] => Array
(
[0] => 846471605959
)
[title] => Array
(
[0] => Test Item 846471605959
)
[imageURL] => Array
(
[0] => https://foo/bar/images/846471605959.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/846471605959
)
[price] => Array
(
[0] => 799.00
)
[sale] => Array
(
[0] => true
)
)
[1] => Array
(
[id] => Array
(
[0] => 414953260545
)
[title] => Array
(
[0] => Test Item 414953260545
)
[imageURL] => Array
(
[0] => https://foo/bar/images/414953260545.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/414953260545
)
[price] => Array
(
[0] => 199.00
)
[sale] => Array
(
[0] => false
)
)
[2] => Array
(
[id] => Array
(
[0] => 684865199812
)
[title] => Array
(
[0] => Test Item 684865199812
)
[imageURL] => Array
(
[0] => https://foo/bar/images/684865199812.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/684865199812
)
[price] => Array
(
[0] => 699.00
)
[sale] => Array
(
[0] => false
)
)
[3] => Array
(
[id] => Array
(
[0] => 987800965761
)
[title] => Array
(
[0] => Test Item 987800965761
)
[imageURL] => Array
(
[0] => https://foo/bar/images/987800965761.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/987800965761
)
[price] => Array
(
[0] => 499.00
)
[sale] => Array
(
[0] => true
)
)
[4] => Array
(
[id] => Array
(
[0] => 005457536677
)
[title] => Array
(
[0] => Test Item 005457536677
)
[imageURL] => Array
(
[0] => https://foo/bar/images/005457536677.jpg
)
[itemURL] => Array
(
[0] => https://foo/bar/item/005457536677
)
[price] => Array
(
[0] => 99.00
)
[sale] => Array
(
[0] => false
)
)
)
)
)
}
)
)
Use a reference variable for the foreach iteration variable, then you can update the element in place.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
if ($item['sale'][0]) {
$item['price'][0] = $new_price;
}
}
If you also need to loop through all the elements in the sale array, add a nested loop.
foreach $data['response'][0]['results'][0]['items'] as &$item) {
foreach ($item['sale'] as $i => $sale) {
if ($sale) {
$item['price'][$i] = $new_price;
}
}
}

Categories