Php combine the values of two array objects - php

I have a oject that contains:and I want combine the content of [0][locationName and [1][locationName into another object/variable so the content would be "Weymouth and Bournemouth". I know there are similar questions on here but my keys in the array have the same name. How would this be done?
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
Many thanks in advance for your time.

Assuming that you have the reference to $destination as an object then perhaps you can try like this:
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
Assuming I recreated the original object correctly
$obj=(object)array(
'destination' => (object)array(
'location' => array(
(object)array('locationName'=>'Weymouth'),
(object)array('locationName'=>'Bournemouth')
)
)
);
The above produces a structure like this:
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
$destination=$obj->destination;
pre( $obj );
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
echo $tmp; // --> outputs Weymouth and Bournemouth

Related

Multidimensional array in pdf report using codeigniter

Below is the code snippet for printing values in a PDF report using codeigniter.
foreach ($data as $key => $getData)
{
$html1 = $this->load->view('pdf/pod2', $getData, true);
//echo $html;exit;
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->setAutoTopMargin = 'stretch';
$pdf->setAutoBottomMargin = 'stretch';
//$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html1);
}
If i do echo '<pre>';print_r($data);exit;
I get the following output
Array
(
[316] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 316
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161052
)
[1] => stdClass Object
(
[invoice] => 161053
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[317] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 317
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161066
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[318] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 318
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161067
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
)
Now the problem is in the pdf report only the last array values is being displayed
i.e 318
Actual scenario what i need is o display three array values but only last value is getting displayed. How to solve this, any help appreciated.

How to check array object and array?

First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);

Merge 2 arrays of objects in PHP [duplicate]

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed last month.
How do you merge array1 and 2?
array1
Array
(
[0] => stdClass Object
(
[name] => bob
[id] => 84569354306
[contacts] => Array
(
[0] => none
)
)
[1] => stdClass Object
(
[name] => jill
[id] => 456745742
[contacts] => Array
(
[0] => none
)
)
)
array2
Array
(
[0] => stdClass Object
(
[name] => bob
[id] => 84569354306
[pid] => 1
[lang] => eng;
[location] =>
)
[1] => stdClass Object
(
[name] => jill
[id] => 456745742
[pid] => 2
[lang] => eng;
[location] =>
)
)
Result array:
Array
(
[0] => stdClass Object
(
[name] => bob
[id] => 84569354306
[pid] => 1
[lang] => eng;
[location] =>
[contacts] => Array
(
[0] => none
)
)
[1] => stdClass Object
(
[name] => jill
[id] => 456745742
[pid] => 2
[lang] => eng;
[location] =>
[contacts] => Array
(
[0] => none
)
)
)
I've tried an array_merge() which seems to add objects next to each other in the array rather than merging the objects.
I'm pretty sure this question is similar to what I need, but I'm having difficulty with the simple foreach loop.
You can cast the two objects to array and then re-cast back to an object. The general syntax is:
$merged = (object)array_merge_recursive((array)$firstObj, (array)$secondObj);
^
| note the recursive in your case
Also, if you are using objects like that maybe you should simply stick to array. It has very little to no sense to do something like that with objects
With multiple items
If you have multiple items you simply need to wrap up my script inside a loop:
function myCustomMerge($array1, $array2) {
assert('count($array1) == count($array2)');
$result = array();
foreach($array1 as $k=>$v) {
$item = array_merge_recursive((array)$array1[$k], (array)$array2[$k]);
$result[]=$item; // use (object)$item if you need objects
}
return $result;
}
Solution without casting
If you prefer not to cast back and forth between array and object you can use get_object_vars():
$obj2props = get_object_vars($obj2);
foreach ($obj2props as $prop => $value) {
$obj1->$prop = $value;
}
return $obj;
Another way of doing it:
function merge_values(){
$list = func_get_args();
while( count( $list ) > 1 ){
$array1 = array_shift( $list );
$array2 = array_shift( $list );
$merged_array = $array1;
foreach( $array2 as $key => $value ){
$merged_array[$key] = array_merge( (array)$value, (array)$merged_array[$key] );
if( is_object( $value ) || is_object( $array1[$key] ) ){
$merged_array[$key] = (object)$merged_array[$key];
}
}
array_unshift( $list, $merged_array );
}
return current( $list );
}
$merged = merge_values( $array1, $array2 );

Get the value in multidimensional arrays php

How to get values of multidimensional array.
Array is as shown below
Array
(
[id] => 1448639278717703
[birthday] => 06/23/1993
[education] => Array
(
[0] => stdClass Object
(
[school] => stdClass Object
(
[id] => 291422000916149
[name] => Vijeta High School
)
[type] => High School
)
[1] => stdClass Object
(
[school] => stdClass Object
(
[id] => 133445980012001
[name] => Vijnana Vihara Residential School
)
[type] => High School
)
[2] => stdClass Object
(
[concentration] => Array
(
[0] => stdClass Object
(
[id] => 111995945484851
[name] => Electronics
)
)
[school] => stdClass Object
(
[id] => 104121832956302
[name] => Vignan University
)
[type] => College
)
)
)
Let take the array as $graphObject then I tried like shown below
$graphObject['education'][0]['school']['name']
But this doesn't worked.
I want to get
School name and type of it.
Concentration name and its school name
Example:
I have to get like
High school: Vijeta High School
High school: Vijnana Vihara Residential School
Concentration in Electronics at Vignan University
Use below code to convert everything into array -
$array = json_decode(json_encode($array),1);
Where $array is your array.
Your innter array contain an object your acces that value like array.
If you want to solve this issue convert your inner ojbect into array and then access the same that you are using.
It's because your array is an object array,
convert it before with this function:
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
then:
$graphObject = objectToArray($graphObject);
print_r ($graphObject['education'][0]['school']['name']);
try
$graphObject['education'][0]->school->name;

change the value of an array in php

i have array in this way
Array
(
[0] => stdClass Object
(
[qa_verified] => 0
)
[1] => stdClass Object
(
[qa_verified] => 1
)
[2] => stdClass Object
(
[qa_verified] => 2
)
)
i need to change into
Array
(
[0] => stdClass Object
(
[qa_verified] => invalidate
)
[1] => stdClass Object
(
[qa_verified] => approve
)
[2] => stdClass Object
(
[qa_verified] => reject
)
)
i have to change the value of qa_verified key depending on the status
0 = invalidate, 1= approve, 2=reject
i tried on array_walk, but unable to get result
any one help me on this
$lookup = array('invalidate', 'approve', 'reject');
array_walk(
$myArray,
function(&$entry) use ($lookup) {
$entry->qa_verified = $lookup[$entry->qa_verified];
}
);
var_dump($myArray);

Categories