I have an array say :
$input = Array
(
[2] => Array
(
[message] => 1 file(s) do not conform to the xxx naming standards.
[type] => warning
[headers] => Array
(
[0] => Array
(
[name] => Package
[property] => zip
)
[1] => Array
(
[name] => Name
[property] => name
)
)
[rows] => Array
(
[0] => Array
(
[zip] => abcd.zip
)
)
)
[111] => Array
(
[message] => Invalid it is
[type] => error
[headers] => Array
(
[0] => Array
(
[name] => ID
[property] => id
)
[1] => Array
(
[name] => Title
[property] => title
)
[2] => Array
(
[name] => Zip
[property] => zip
)
)
[rows] => Array
(
[0] => Array
(
[id] => abcd
[title] => title
[zip] => abcd
)
)
)
)
Then I calculate length as below :
$length = count($this->view->feedback);
$i= 0;
Now I am running a foreach loop as
foreach ($input as $operation)
{
if($operation['type'] == 'error')
{
$output = array_slice($operation, -$i, $length);
}
$i++;
}
The above array_slice() method outputs as below :
$output =
Array
(
[rows] => Array
(
[0] => Array
(
[id] => 214501
[title] => SRM- 3860(Res-2)
[zip] => SRM-3860(Test_2).zip
)
)
)
My DESIRED/REQUIRED output is :
$output =
Array
(
[111] => Array
(
[message] => Invalid it is
[type] => error
[headers] => Array
(
[0] => Array
(
[name] => ID
[property] => id
)
[1] => Array
(
[name] => Title
[property] => title
)
[2] => Array
(
[name] => Zip
[property] => zip
)
)
[rows] => Array
(
[0] => Array
(
[id] => abcd
[title] => title
[zip] => abcd
)
)
)
)
How can I achieve this ?
Is something wrong with my array_slice method ?
What about this :
$my_desired_output = [];
foreach ($input as $key => $operation)
{
if($operation['type'] == 'error')
{
$my_desired_output[$key] = $operation;
}
}
Related
I have following array from google api and wanted to get id only. How do i get id's from following. when i try to get size it gives me size zero.
Google_Service_Drive_FileList Object ( [collection_key:protected] => items
[internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected]
=> Google_Service_Drive_DriveFile [itemsDataType:protected] => array [kind] =>
[nextLink] => [nextPageToken] => [selfLink] => [modelData:protected] => Array (
[items] => Array ( [0] => Array ( [id] => 0B0OnHwH_cQckeWZPdXFyRU5aMGs ) [1] =>
Array ( [id] => 0B0OnHwH_cQckaUVORkZaM2NoRXM ) [2] => Array ( [id] =>
1kCQLhEgzgeKO-L57ISWjQL4ctkxT4Gq2wrdzDFbrcac ) [3] => Array ( [id] =>
0B0OnHwH_cQckc3RhcnRlcl9maWxl ) [4] => Array ( [id] => 1-Yhs92vZnvUNArwAcZJZ9xa-
fXZ7ZgRrADyF-ikG1gU ) ) ) [processed:protected] => Array ( ) ) Array ( )
if you mean the object attributes, you can do
$values = array();
$keys = array();
foreach($object as $key=>$value)
{
array_push($keys,$key);
array_push($values,$value);
}
This will give you each object attribute and its corresponding value
The array is received from the facebook api and i am not able to extract the likes array from the array,Please help me
[data] => Array (
[0] => Array (
[message] => Hello
[id] => 729659027165160_729651713832558
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 729659027165160
)
)
[paging] => Array (
[cursors] => Array (
[after] => NzI5NjU5MDI3MTY1MTYw
[before] => NzI5NjU5MDI3MTY1MTYw
)
)
)
)
[1] => Array (
[id] => 729659027165160_718306454967084
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1719747118259908
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTcxOTc0NzExODI1OTkwOA==
[before] => MTcxOTc0NzExODI1OTkwOA==
)
)
)
)
[2] => Array (
[id] => 729659027165160_541135166017548
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[3] => Array (
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[id] => 729659027165160_520677651396633
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
[1] => Array (
[id] => 806391372817118
)
[2] => Array (
[id] => 928633297192567
)
[3] => Array (
[id] => 824812004311172
)
[4] => Array (
[id] => 10207344532684729
)
[5] => Array (
[id] => 1188171664544003
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE4ODE3MTY2NDU0NDAwMw==
[before] => MTE2MjQyODk3MDQ1Mzg0Mg==
)
)
)
)
[4] => Array (
[id] => 729659027165160_110578795739856
[likes] => Array (
[data] => Array (
[0] => Array (
[id] => 1162428970453842
)
)
[paging] => Array (
[cursors] => Array (
[after] => MTE2MjQyODk3MDQ1Mzg0Mg==
[before] => MTE2MjQyO
)
)
)
)
)
I am able to extract the id from the above array,but unable to extract the count of likes and message.
Try this code, it iterates in your array and stores all ids and stores the message, the likes Array and the number of likes only if they exist (Supposing that your array is named $myarray):
$result = array();
foreach($myarray['data'] as $data){
$item = array();
$item['id'] = $data['id'];
if( isset($data['message']) || isset($data['likes']) ){
if(isset($data['message'])) $item['message'] = $data['message'];
if(isset($data['likes'])) {
$item['likes'] = array();
foreach($data['likes']['data'] as $like){
$item['likes'][] = $like['id'];
}
$item['countlikes'] = count( $data['likes']['data'] );
}
}
$result[] = $item;
}
print_r($result);
With your example Array the result will be:
Array
(
[0] => Array
(
[id] => 729659027165160_729651713832558
[message] => Hello
[likes] => Array
(
[0] => 729659027165160
)
[countlikes] => 1
)
[1] => Array
(
[id] => 729659027165160_718306454967084
[likes] => Array
(
[0] => 1719747118259908
)
[countlikes] => 1
)
[2] => Array
(
[id] => 729659027165160_541135166017548
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
[3] => Array
(
[id] => 729659027165160_520677651396633
[message] => Panipaata leni prathivaadu philosophy cheppevade.... Wish Facebook introduce an unlike button soon!!!!
[likes] => Array
(
[0] => 1162428970453842
[1] => 806391372817118
[2] => 928633297192567
[3] => 824812004311172
[4] => 10207344532684729
[5] => 1188171664544003
)
[countlikes] => 6
)
[4] => Array
(
[id] => 729659027165160_110578795739856
[likes] => Array
(
[0] => 1162428970453842
)
[countlikes] => 1
)
)
$myArray = Array
(
[Header] => Array
(
[#attributes] => Array
(
[ShouldRecordPerformanceTime] => False
[Timestamp] => 2014-06-11 16:33:20:04501
[ReferenceID] => 8fc07483-94ff-4a70-a8fa-d54292598592
[RecentlyTime] => 2014-06-11 16:33:08
[AccessCount] => 30
[CurrentCount] => 2
[ResetTime] => 2014-06-11 16:34:08
[ResultCode] => Success
)
)
[HotelResponse] => Array
(
[OTA_HotelSearchRS] => Array
(
[#attributes] => Array
(
[TimeStamp] => 2014-06-11T16:33:19.8857903+08:00
[Version] => 1.0
[PrimaryLangID] => zh
)
[Properties] => Array
(
[Property] => Array
(
[#attributes] => Array
(
[BrandCode] => 110
[HotelCode] => 443707
[HotelCityCode] => 2
[HotelName] => rujiakuaijie
[AreaID] => 112
[HotelId] => 443707
)
[VendorMessages] => Array
(
)
[Position] => Array
(
[#attributes] => Array
(
[Latitude] => 31.235452
[Longitude] => 121.447776
[PositionTypeCode] => 502
)
)
[Address] => Array
(
[AddressLine] => changpin road
[CityName] => SHANGHAI
[PostalCode] => 200040
)
[Award] => Array
(
[0] => Array
(
[#attributes] => Array
(
[Provider] => HotelStarRate
[Rating] => 2
)
)
[1] => Array
(
[#attributes] => Array
(
[Provider] => CtripStarRate
[Rating] => 2
)
)
[2] => Array
(
[#attributes] => Array
(
[Provider] => CtripRecommendRate
[Rating] => 1.5
)
)
[3] => Array
(
[#attributes] => Array
(
[Provider] => CtripCommRate
[Rating] => 0
)
)
)
[RelativePosition] => Array
(
[0] => Array
(
[#attributes] => Array
(
[Distance] => 1.58
[UnitOfMeasureCode] => 2
[Name] => changshou
)
)
[1] => Array
(
[#attributes] => Array
(
[Distance] => 44.729
[UnitOfMeasureCode] => 2
[Name] => pudong
)
)
[2] => Array
(
[#attributes] => Array
(
[Distance] => 12.88
[UnitOfMeasureCode] => 2
[Name] => hongqiao
)
)
[3] => Array
(
[#attributes] => Array
(
[Distance] => 11.208
[UnitOfMeasureCode] => 2
[Name] => shanghai
)
)
[4] => Array
(
[#attributes] => Array
(
[Distance] => 15.571
[UnitOfMeasureCode] => 2
[Name] => trainstation
)
)
[5] => Array
(
[#attributes] => Array
(
[Distance] => 4.167
[UnitOfMeasureCode] => 2
[Name] => central
)
)
[6] => Array
(
[#attributes] => Array
(
[Distance] => 3.836
[UnitOfMeasureCode] => 2
[Name] => museum
)
)
)
[TPA_Extensions] => Array
(
[Zone] => Array
(
[ZoneType] => Array
(
[#attributes] => Array
(
[ZoneID] => 981
[ZoneName] => commercial
)
)
)
)
)
)
[Success] => Array
(
)
)
)
)
The array look like this ,I used the sample of php.net to loop through the array to get key and value
0 : a
0 : subA
1 : subB
0 : subsubA
1 : subsubB
0 : deepA
1 : deepB
unfortunately I got nothing response, anyone can help will be appaeciate
$iterator = new RecursiveArrayIterator($myArray);
iterator_apply($iterator, 'traverseStructure', array($iterator));
function traverseStructure($iterator) {
while ( $iterator -> valid() ) {
if ( $iterator -> hasChildren() ) {
traverseStructure($iterator -> getChildren());
}
else {
echo $iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL;
}
$iterator -> next();
}
}
?>
function DFS($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$this->DFS($v);
}
else echo '<br />'. $k. ' - '. $v;
}
}
im using cakephp 1.3. Im trying to sort an array using Set::sort() function but is not working.. any idea on how to do this? below is the array im using.
Array (
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
[1] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
)
)
im sorting the array using this :
$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');
im expecting an output like this:
Array
(
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[1] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
[2] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
)
)
The sorting does not take in effect.how can i do this? any idea?
Using only Set::sort() its no doable. You can you this:
$result = array();
foreach($a as $arr) {
$res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
$result[] = array(
'Group' => $arr['Group'],
'Member' => $res
);
}
pr($result);
Array (
[0] => Array (
[PACKAGE] => Array (
[0] => Array (
[ATTRIBUTES] => Array (
[ID] => 0
)
[ZIPORIGINATION] => Array (
[0] => Array (
[VALUE] => 32751
)
)
[ZIPDESTINATION] => Array (
[0] => Array (
[VALUE] => 55967
)
)
[POUNDS] => Array (
[0] => Array (
[VALUE] => 0
)
)
[OUNCES] => Array (
[0] => Array (
[VALUE] => 9
)
)
[CONTAINER] => Array (
[0] => Array (
[VALUE] => Flat Rate Box
)
)
[SIZE] => Array (
[0] => Array (
[VALUE] => REGULAR
)
)
[ZONE] => Array (
[0] => Array (
[VALUE] => 6
)
)
[POSTAGE] => Array (
[0] => Array (
[MAILSERVICE] => Array (
[0] => Array (
[VALUE] => Priority Mail<sup>®</sup> Medium Flat Rate Box
)
)
[RATE] => Array (
[0] => Array (
[VALUE] => 11.35
)
)
)
)
)
)
)
)
Array (
[0] => price Object (
[mailservice] => Priority Mail<sup>®</sup> Medium Flat Rate Box
[rate] => 11.35
)
)
...yeah. I need to search through these and pull out the RATE value, which here is 11.35.
For/each loops have failed me so far, any other ideas?
Here is a function to do this for you:
findKey($myarray,$searchKey) {
foreach($myarray as $key=>$value) {
if($key==$searchKey) return $value;
if(is_array($value) || is_object($value)) {
$returned = findKey($value,$searchKey);
if($returned) return $returned;
}
}
return false;
}
// call it like this:
findKey($yourArray,"rate")