How would I select certain value from this element - php

I would like to select the Image Url which is ('http://img.site.com//uploaded/articleThumbnails/DA0AE88C-AB01-4919-9D97-5FDE884A8D9E.jpeg') from the array below, but do not know how.
Array
(
[0] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => meta
[attr] => Array
(
[itemprop] => image
[content] => http://img.site.com//uploaded/articleThumbnails/DA0AE88C-AB01-4919-9D97-5FDE884A8D9E.jpeg
)
[children] => Array
(
)
[nodes] => Array
(
)
[parent] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => head
[attr] => Array
(
)
[children] => Array
(
[0] => simple_html_dom_node Object
(
[nodetype] => 1
[tag] => script
[attr] => Array
(
[type] => text/javascript
)
[children] => Array
(
)

store your entire array to a variable just for eg. call it as $datas
foreach($datas as $data)
{
echo $data->attr['content'];
}

If you have multiple element then use this
foreach ($yourArray as $val)
{
foreach ($val->attr as $val2)
{
$getyourURL[] = $val2['content'];
}
}
$getyourURL contain your all URLs

Try this
echo $arr[0][attr][content];

Use This
foreach($datas as $data)
{
echo $data[attr][content];
}
or if you want only this field without all in loop use this.
echo $datas[0][attr][content];

Related

Get the difference between one sub-Array or multiple sub-Arrays in a multidimensional Array

I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.
For Example:
In Array [1] there is just one sub Array [example]
In Array [2] there are two sub Arrays [example]
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
Now to get the [value] from the first Array I would try:
foreach ($content as $example) {
echo($content['example']['value']);
}
And to get each [value] from the second Array I would try:
foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}
So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?
Something like:
if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}
I simply want a method to get all values called [value] out of the array.
Thank you in advance!
The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
But if you don't have such option - then use a simple check:
foreach ($content as $item) {
// here check if your `$item` has an `value` subkey under `example` key
if (array_key_exists('value', $item['example'])) {
echo($item['example']['value']);
} else {
foreach ($item['example'] as $values) {
echo ($values['value']);
}
}
}
Assuming that your final dimension allways as a 'value' node:
function arrayIterate($array){
foreach ($content as $example) {
if(!isset($example['value'])){
arrayIterate($example);
}else{
echo($example['value']);
}
}
}

Unset an array using php

I have a problem like i have a multidimensional array and i want to remove a parent array if an inner array contains 'edit'
the array is like this
[fields] => Array
(
[0] => Array
(
[header] => Array
(
[fieldName] => edit
[displayName] => Edit
[width] => 40
[group] => 0,1,2,3
[cssClass] => dgAssetsCentered
)
[visibility] => Array
(
[showOnStart] => 1
[editable] =>
)
[cell] => Array
(
[type] => link
[params] => Array
(
[label] => Edit
)
)
[sort] => Array
(
[sortable] => 1
)
[validator] => Array
(
[name] =>
[params] =>
)
)
[1] => Array
(
[header] => Array
(
[fieldName] => OFFERID
[displayName] => Offer Id
[group] => 0
i want to remove array 0 because inner array contains 'edit' i am able to delete [header] array not able to delete [0] array.please suggest.
thanks
alex.
unset(fields); will clear the entire array.
unset(fields[0]); will clear the element 0 inside fields array
foreach($fields as $key => $field)
{
if(isset($field['header']['fieldName']) && $field['header']['fieldName'] == 'edit')
unset($field[$key]);
}
UPD:
Also, if you wanna remove array, if any element equal 'edit', try this algorithm:
foreach($fields AS $key => $array)
{
foreach($array as $innered)
{
if(array_search('edit', $innered))
{
unset($fields[$key]);
break;
}
}
}

Looping through array contain 3 classes

I am having trouble getting my head around how to loop through stdClasses.
Printing the array gives me the following:
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1003
[entity_id] => 0
[redirect_url] => http://yahoo.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1002
[entity_id] => 0
[redirect_url] => http://google.com
[type] => Image
)
)
[total_count] => 1
)
Array
(
[piggyback] => Array
(
[0] => stdClass Object
(
[id] => 1001
[entity_id] => 0
[redirect_url] => http://bing.com
[type] => Image
)
)
[total_count] => 1
I am trying to loop though with the following and print out a value (id) but I keep getting nothing.
foreach ($piggies_array as $key => $value) {
echo $piggies_array[$key]['id'];
}
foreach ($piggies_array as $key => $value) {
if (is_array($value)){
echo $value[0]->id;
}
}
I think you need:
for ($i = 0; $i < count($piggies_array); $i++) {
echo $piggies_array[$i]['piggyback'][0]->id;
}
...assuming we can only see part of your output ;)
try replace it to echo $value->id;

php move nodes to parent array

How could I move all nodes of the 'fields' array into its parent array '113', whilst unsetting 'fields' ?
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[fields] => Array
(
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
)
Assuming your top level array ($something['a']) is the variable $a:
foreach($a as $key => $values){
if(isset($values['fields']))
{
$a[$key] = array_merge($a[$key], (array) $values['fields']);
unset($a[$key]['fields']);
}
}
Alternatively, if you dont want to hit every array element in $a you can just remove the loop and substitute $values with $a[113] and $key with 113.
Also note the casting for the fields element to an array, jsut in case it isnt one with (array) $values['fields']
If you can make this array like this:
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
try to use this code:
$array['a'][113]['jr_streetaddress'] = $array['a'][113]['fields']['jr_streetaddress'];
$array['a'][113]['jr_towncity'] = $array['a'][113]['fields']['jr_towncity'];
unset($array['a'][113]['fields']);

how to extract the exact data from an array in php

i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}

Categories