How to change array format in PHP? - 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?

Related

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.

convert cavtivedataprovider to array with specific form

im using yii1 on my application.
i want to convert from cActivedataProvider to array
this is the code
$dataSS = new CActiveDataProvider('category', array(
'criteria' => array(
'condition' => 'menu=:menu',
'params' => array(':menu' => $menu),
),
'pagination' => false
));
$dataMenu = array();
foreach ($dataSS->getData() as $record) {
$dataMenu[] = array(
'label' => $record->name,
'url' => '#',
);
}
this is the result :
Array (
[0] => Array ( [label] => Food and Drink [url] => # )
[1] => Array ( [label] => Sleman [url] => # )
)
the result that i expected :
Array (
Array ( 'label' => 'Food and Drink', 'url' => '#' ) ,
Array ( 'label' => 'Sleman', 'url' => '#' ) ,
)
any suggestion?
Finally get the answer,
this is really my bad because i call the function on wrong way.
This is the wrong way :
'items' =>array(Category::model()->getMenu("2");),
and this is the correct way :
'items' =>Category::model()->getMenu("2"),

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);

Sort Multidimentional Array in PHP according a part of an array

I have the following array:
items = array(
'note' => array(),
'text' => array(),
'year' => array()
)
So I have:
[note] => Array
(
[0] => 'note1'
[1] => 'note2'
[2] => 'note3'
),
[text] => Array
(
[0] => 'text1'
[1] => 'text2'
[2] => 'test3'
),
[year] => Array
(
[0] => '2002'
[1] => '2000'
[2] => '2011'
)
And I would like to arrange the above arrays by year. but when moving elements I would like to move the corresponding elements in other arrays(note,text).
For example:
[note] => Array
(
[2] => 'note3'
[0] => 'note1'
[1] => 'note2'
),
[text] => Array
(
[2] => 'text3'
[0] => 'text1'
[1] => 'test2'
),
[year] => Array
(
[2] => '2011'
[0] => '2002'
[1] => '2000'
)
I would first extract the year part and sort it by value, while still maintaining the key, using arsort():
$yearData = $array['year'];
arsort($yearData);//sort high-to-low by value, while maintain it's key.
Finally, sort the data using this newly sorted year:
$newArray['note'] = array();
$newArray['text'] = array();
$newArray['year'] = array();
foreach($yearData as $key => $value){
$newArray['note'][$key] = $array['note'][$key];
$newArray['text'][$key] = $array['text'][$key];
$newArray['year'][$key] = $array['year'][$key];
}
FYI, there are a bunch of functions that deal with sorting arrays in PHP.
I think a better organization to your array would be something like this:
[0] => Array(
'note' => note1, 'text' => 'text1', 'year' => '2002)
[1] => Array(
'note' => note2, 'text' => 'text2', 'year' => '2000)
[2] => Array(
'note' => note3, 'text' => 'text4', 'year' => '2011)
This way, each related item stays together and it's easier to sort them by the desired type.
$items = array(
array(
'note' => value,
'text' => value,
'year' => value
),
array(
'note' => value,
'text' => value,
'year' => value
)
)

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