How to access *key value in array using php - php

i have below array with key combined * i want get the value using array key
i got response from api
Array (
[pos] => Bitpay\Token Object
(
[token:protected] => 8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm
[resource:protected] =>
[facade:protected] => pos
[createdAt:protected] =>
[policies:protected] => Array
(
)
[pairingCode:protected] =>
[pairingExpiration:protected] =>
)
[pos/invoice] => Bitpay\Token Object
(
[token:protected] => 4XyeM78xv6ywzTB3Cc2yak7Bb9duAW1DaCu5XDAVuSEQ
[resource:protected] =>
[facade:protected] => pos/invoice
[createdAt:protected] =>
[policies:protected] => Array
(
)
[pairingCode:protected] =>
[pairingExpiration:protected] =>
)
)
after i convert array i got below array structure, I want get a token:protected value, How can i get this
Array(
[*token] => 8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm
[*resource] =>
[*facade] => pos
[*createdAt] =>
[*policies] => Array
(
)
[*pairingCode] =>
[*pairingExpiration] =>
)
I want take get a token value, Any one Please help.

Using quotes should make it all possible:
<?php
$myArray = array(
'*token' => '8Q13oFMiBLBcqFCK5wWfhUYxxhcpkq4C6Xqh6ipgbxTm',
'*resource' => '',
'*facade' => 'pos',
'*createdAt' => '',
'*policies' => array
(
),
'*pairingCode' => '',
'*pairingExpiration' => '',
);
print $myArray['*token'];
?>

Related

How to change array format in PHP?

I have simple question:
How to change this array
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
to this array?
array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' )
Why I am asking for that:
$slugs = wp_get_post_terms($post->ID,'ml_patternsCustomTaxonomy',['fields'=>'slugs']);
returns
Array ( [0] => taxonomy-term-1 [1] => taxonomy-term-2 [2] => taxonomy-term-3 )
but
array(
'title' => $title,
'content' => trim($patternContent),
'categories' => $slugs
)
doesn't work, and
//
'categories' => array( 'taxonomy-term-1', 'taxonomy-term-2', 'taxonomy-term-3' ),
works.
Why?

Count Total Indexes of Array in PHP

I have an array with the following structure:
Array
(
[DigitalAssets] => Array
(
[0] => Array
(
[PartNumber] => 0276S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0576s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:10.28
)
[1] => Array
(
[PartNumber] => 0437S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0437s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:11.687
)
[2] => Array
(
[PartNumber] => 0574S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0574s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:12.593
)
I want to count the total Indexes of array so that I will run loop accordingly. I used Count($array) and Count ($array,RECURSIVE) but it cannot return the correct total number of indexes.
Can only guide that how to do this?
Thanks
If You want to count DigitalAssets direct children You can do this
count($array['DigitalAssets'])
Here are some tests how count should work.
Code is:
$test = array
(
'DigitalAssets' => array
(
0 => array(
'PartNumber' => '0276S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0576s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:10.28'
),
1 => array(
'PartNumber' => '0437S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0437s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:11.687'
),
2 => array
(
'PartNumber' => '0574S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0574s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:12.593'
)
)
);
echo count($test['DigitalAssets'])." ".count($test['DigitalAssets'], 0)." ".count($test['DigitalAssets'], 1);
exit();
Result in my case:
3 3 18
This means taht in my case mode in count is set to 0 by default so i will get only first level counted. If i set mode to 1 i will get all nested items counted as well. This should clear things up for You.

php array for xml soap request error "XML seems to be incomplete or wrong"

I am trying to make request from PHP soap client for Juniper lot of hotels services but I am getting response with error (XML seems to be incomplete or wrong) the request XML must be like that
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://www.juniper.es/webservice/2007/">
<soapenv:Header/>
<soapenv:Body>
<HotelAvail>
<HotelAvailRQ Version="1.1" Language="en">
<Login Email="user#mydomain.com" Password="pass"/>
<Paxes>
<Pax IdPax="1">
<Age>31</Age>
</Pax>
<Pax IdPax="2">
<Age>29</Age>
</Pax>
<Pax IdPax="3">
<Age>5</Age>
</Pax>
</Paxes>
<HotelRequest>
<SearchSegmentsHotels>
<SearchSegmentHotels Start="2014-04-24" End="2014-04-28" DestinationZone="1953"/>
<CountryOfResidence>ES</CountryOfResidence>
</SearchSegmentsHotels>
<RelPaxesDist>
<RelPaxDist>
<RelPaxes>
<RelPax IdPax="1"/>
<RelPax IdPax="2"/>
<RelPax IdPax="3"/>
</RelPaxes>
</RelPaxDist>
</RelPaxesDist>
</HotelRequest>
<AdvancedOptions>
<ShowAllCombinations>1</ShowAllCombinations>
<ShowHotelInfo>1</ShowHotelInfo>
<ShowBreakdownPrice>1</ShowBreakdownPrice>
</AdvancedOptions>
</HotelAvailRQ>
</HotelAvail>
</soapenv:Body>
</soapenv:Envelope>
and my request array after print it by print_r function is like that
Array (
[HotelAvailRQ] => Array (
[Version] => 1.1 [Language] => en
[Login] => Array (
[Email] => xxxxxxxxx[Password] => xxxxxxxxxx
)
[Paxes] => Array (
[0] => Array (
[Pax] => Array (
[IdPax] => 1 [Age] => 15
)
)
[1] => Array (
[Pax] => Array (
[IdPax] => 2 [Age] => 30
)
)
[2] => Array (
[Pax] => Array (
[IdPax] => 3 [Age] => 50
)
)
)
[HotelRequest] => Array (
[SearchSegmentsHotels] => Array (
[Start] => 2017-11-20 [End] => 2017-11-25 [DestinationZone] => 1953 [CountryOfResidence] => ES [Boards] => Array (
[Board] => Array (
[Type] => AD
)
)
)
[RelPaxesDist] => Array (
[RelPaxDist] => Array (
[RelPaxes] => Array (
[RelPax] => Array (
[0] => Array (
[IdPax] => 1
)
[1] => Array (
[IdPax] => 2
)
[2] => Array (
[IdPax] => 3
)
)
)
)
)
)
[AdvancedOptions] => Array (
[ShowHotelInfo] => 1
)
)
)
i need to make the request array to be accepted when it is converted to XML bt the soap client request
there is the array printed by var_export function
array ( 'HotelAvailRQ' => array ( 'Version' => '1.1', 'Language' => 'en', 'Login' => array ( 'Email' => 'xxxxxxxxx', 'Password' => 'xxxxxxxxxxxx', ), 'Paxes' => array ( 0 => array ( 'Pax' => array ( 'IdPax' => 1, 'Age' => '15', ), ), 1 => array ( 'Pax' => array ( 'IdPax' => 2, 'Age' => '30', ), ), 2 => array ( 'Pax' => array ( 'IdPax' => 3, 'Age' => '50', ), ), ), 'HotelRequest' => array ( 'SearchSegmentsHotels' => array ( 'Start' => '2017-11-20', 'End' => '2017-11-25', 'DestinationZone' => '1953', 'CountryOfResidence' => 'ES', 'Boards' => array ( 'Board' => array ( 'Type' => 'AD', ), ), ), 'RelPaxesDist' => array ( 'RelPaxDist' => array ( 'RelPaxes' => array ( 'RelPax' => array ( 0 => array ( 'IdPax' => 1, ), 1 => array ( 'IdPax' => 2, ), 2 => array ( 'IdPax' => 3, ), ), ), ), ), ), 'AdvancedOptions' => array ( 'ShowHotelInfo' => true, ), ), )
There is not better solution today thatn using a WSDL to PHP generator that ensure your correctly structure your PHP request.
I can only advice you to use the PackageGenerator project.
If it does not work, it can come from the fact the the native SoapClient class does not handle correctly this sort of WSDL so you'll have to override the SoapClient::__doRequest method.

multidimensional array to JSON return NULL in PHP

I have an array, i want to convert this to json, but it returns null for a field,
here is my array:
[workname] => IŞIKLAR MÜHENDİSLİK
[workno] => 22330
[workdate] => Array
(
[0] => Array
(
[date] =>
[type] => ELEKTRİK
)
[1] => Array
(
[date] => 31.12.2007
[type] => ELEKTRİK
)
)
when i convert this to json, i get workdate element as null.
{"workname":"IŞIKLAR MÜHENDİSLİK","workno":"22330","workdate":null}
$arr = array('workname' => 'IŞIKLAR MÜHENDİSLİK','workno' => 22330,'workdate' => array('0' => array('date' => '','type' => 'ELEKTRİK'),'1' => array('date' => '31.12.2007','type' => 'ELEKTRİK')));
echo json_encode($arr);
outputs
{"workname":"I\u015eIKLAR M\u00dcHEND\u0130SL\u0130K","workno":22330,"workdate":[{"date":"","type":"ELEKTR\u0130K"},{"date":"31.12.2007","type":"ELEKTR\u0130K"}]}
you can check it here http://sandbox.onlinephpfunctions.com/code/68c91d260a0a18d584dab871f56dd7c97482ca04
$arr=array(
'workname' => 'IŞIKLAR MÜHENDİSLİK',
'workno' => 22330,
'workdate' => Array (
Array (
'date' => '',
'type' => 'ELEKTRİK'
),
Array(
'date' => '31.12.2007',
'type' => 'ELEKTRİK'
)
)
);
echo json_encode($arr);

MongoDB + PHP: How to push item to array with a spcecific key?

In MongoDB-PHP I am using the following example code to push a new entry to the end of an array inside a collection...
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->save(
array( 'someid' => $someid),
array( '$push' => array( 'data' => $data ))
);
This returns keys like 0,1,2,3....
ie.
[someid] => somesupercoolid123
[data] => Array
(
[0] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[1] => Array(...)
[2] => Array(...)
)
What I want to do is store YearWeekNumber as the key like this...
[someid] => somesupercoolid123
[data] => Array
(
[201205123] => Array
(
[domain] => superduperyoyo.com
[number] => 123
[week] => 5
[year] => 2012
)
[201206123] => Array(...)
[201207123] => Array(...)
)
How do you save/update the key along with the new entry? I am assuming you can't use $push. That you just use .save or .update but how do you pass the key?
You'd do this by using $set:
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$db->domains->update(
array( 'someid' => $someid),
array( '$set' => array( 'data' => array( 201205123 => $data )))
);
I would however not recommend doing this. It's better to set another key with this "201205123" value as otherwise you wouldn't be able to do range queries on this value or set an index.
$data = array(
"domain"=>"superduperyoyo.com",
"number"=>123,
"week"=>5,
"year"=>2012
);
$update = array(
'$push' => array('data.201205123' => $data )
);

Categories