So, I have following db result:
Array
(
[0] => stdClass Object
(
[id] => 1
[user] => 1
[img] => 2016/02/img_8488.jpg
[url] => /p=?44
[sent_date] => 2016-02-13 00:00:00
)
[1] => stdClass Object
(
[id] => 2
[user] => 185
[img] =>
[url] => /?p=54
[sent_date] => 2016-02-06 00:00:00
)
)
How would I remove [id] and [sent_date] from the query result?
I am not sure if I am using unset right.
unset($results[0]['id']);
$reindex = array_values($results);
$objectarray = $reindex;
Instead of removal or unset you can create a new array;
$i = 0;
$newResult = array();
foreach($result as $value){
$newResult[$i]["user"] = $value->user;
$newResult[$i]["img"] = $value->img;
$newResult[$i]["url"] = $value->url;
$i++;
}
print_r($newResult);
$newResult will return the new array and your original array remains same you can use it if you need.
Or removal of indexes is must required than use unset inside the foreach loop as:
unset($value->id);
unset($value->sent_date);
Side note:
Also keep in mind you can not use it as $value["id"] becuase its a property not an array index.
Use unset($results[0]->id); and unset($results[0]->sent_date) instead and it should work. If you want to do this in all of the array objects:
for($i = 0; $i<sizeof($results); $i++)
{
unset($results[$i]->id);
unset($results[$i]->sent_date);
}
Related
How to search an array of objects by keyword and get that object set if exists.
For example --
array(
[0] => Object
(
[id] => 123
[label] => 'Jone Due'
[title] => 'Bangladeshi Laravel Expert'
)
[1] => Object
(
[id] => 234
[label] => 'Jone Due'
[title] => 'Bangladeshi Singer'
)
[2] => Object
(
[id] => 345
[label] => 'Jone Due'
[title] => 'Bangladeshi Actor'
)
....
);
I want to search title with keyword Laravel, and result i want to get --
array(
[0] => Object
(
[id] => 123
[label] => Jone Due
[title] => Bangladeshi Laravel Expert
)
);
Is it possible?
Try next. It works for me:
$i = 0; // counter
$ar = []; // array of indexes of success objects
$ar2 = []; // result array of objects which title has 'Laravel' inside
// $obj_ar must be consists of objects (it should has some checking code for that requirement)
// filling an array of indexes $ar
foreach ($obj_ar as $obj_1){
if (strstr($obj_1->title,'Laravel')) array_push ($ar, $i);
$i++;
}
// building a result array of objects
$count_ar = count($ar);
if ($count_ar>0) {
for($o = 0; $o < $count_ar; $o++){
array_push ($ar2, $obj_ar[$o]);
}
}
// result array of objects
echo '<pre>';
print_r($ar2);
echo '</pre>';
or a bit faster way:
$i = 0; // counter
$ar2 = []; // result array of objects which title has 'Laravel' inside
// $obj_ar must be consists of objects (it should has some checking code for that requirement)
// filling an array of indexes $ar
foreach ($obj_ar as $obj_1){
if (strstr($obj_1->title,'Laravel')) array_push ($ar2, $obj_ar[$i]);
$i++;
}
// result array of objects
echo '<pre>';
print_r($ar2);
echo '</pre>';
I don't know what to do to get this done what would like to do. I tried multiple approaches, e.g. I used array_map, array_walk, nested foreach loops with get_object_vars and I worked with json_decode/encode and so on. I always come a little bit further but never reach my goal and I would like to get some guidance from you
Basically when you see the array below, how would you proceed when you want to change some value in the path array for multiple values in the array itself?
My questions:
1) Is it right that I must convert both nested objects to an array first or is this not nesessary to do this? I mean I always get some type conversion error which tells me that I either have everything as an object or array. Is this right?
2) If this mistery is solved, which php array function is the appropriate one to change values in an array(/object)? As I have written above, I tried so many and I don't see the trees in the woods anymore. Which one do you suggest to me to use in a foreach loop?
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pictures
[2] => food
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pictures
[2] => vacations
[3] => rome
)
)
)
)
I would suggest that,
you create an array with keys as new path and value as old path (
path to be replaced).
Loop you path array and check if it is available in above defined array.
If available replace it with key of above defined array.
For example
// array defined as point 1
$change_path_array= array('pics'=>'pictures','meal'=>'food');
// $array is your array.
foreach ($array as $value) {
// loop you path array
for($i=0;$i<count($value->doc->path);$i++){
// check if the value is in defined array
if(in_array($value->doc->path[$i],$change_path_array)){
// get the key and replace it.
$value->doc->path[$i] = array_search($value->doc->path[$i], $change_path_array);
}
}
}
Out Put: picture is replaced with pics and food with meal
Array
(
[0] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Bob
[1] => pics
[2] => meal
)
)
)
[1] => stdClass Object
(
[doc] => stdClass Object
(
[path] => Array
(
[0] => Alice
[1] => pics
[2] => vacations
[3] => rome
)
)
)
)
You can modify the code to check casesensitive.
Example of changing all pictures to photos:
$doc1 = new \stdClass;
$doc1->doc = new \stdClass;
$doc1->doc->path = array('Bob', 'pictures', 'food');
$doc2 = new \stdClass;
$doc2->doc = new \stdClass;
$doc2->doc->path = array('Alice', 'pictures', 'vacations', 'rome');
$documents = array($doc1, $doc2);
/* change all 'pictures' to 'photos' */
foreach ($documents as &$doc) {
foreach ($doc->doc->path as &$element) {
if ($element == 'pictures') {
$element = 'photos';
}
unset($element);
}
unset($doc);
}
print_r($documents);
You can do it like this:
for($i = 0; $i < count($arr); $i++){
$path_array = $arr[$i]->doc->path;
// do your modifications for [i]th path element
// in your case replace all 'Bob's with 'Joe's
$path_array = array_map(function($paths){
if($paths == 'Bob') return 'Joe';
return $paths;
}, $paths_array);
$arr[$i]->doc->path = $path_array;
}
I've got an object, containing an array of objects, containing an array of values:
stdClass Object (
[devices] => Array (
[0] => stdClass Object (
[location] => 1
[delegate] =>
[type] => 1
[id] => 1234
[IP] => 1.2.3.4
[name] => host1
[owner] => user6
[security] => 15
)
[1] => stdClass Object (
[location] => 2
[delegate] =>
[type] => 1
[id] => 4321
[IP] => 4.3.2.1
[name] => host2
[owner] => user9
[security] => 15
)
)
)
I want to extract just the id and name into an array in the form of:
$devices['id'] = $name;
I considered using the array_map() function, but couldn't work out how to use it... Any ideas?
This will generate you a new array like I think you want
I know you says that delegate is an object but the print does not show it that way
$new = array();
foreach($obj->devices as $device) {
$new[][$device->id] = $device->name;
}
Would something like this not work? Untested but it cycles through the object structure to extract what I think you need.
$devices = myfunction($my_object);
function myfunction($ob){
$devices = array();
if(isset($ob->devices)){
foreach($ob->devices as $d){
if(isset($d->delegate->name && isset($d->delegate->id))){
$devices[$d->delegate->id] = $d->delegate->name;
}
}
}
return($devices);
}
Im usually using this function to generate all child and parent array stdclass / object, but still make key same :
function GenNewArr($arr=array()){
$newarr = array();
foreach($arr as $a=>$b){
$newarr[$a] = is_object($b) || is_array($b) ? GenNewArr($b) : $b ;
}
return $newarr;
}
I saved the result of a CURL expression into variable $data. When I print this value using print_r($data), It gives me like that
stdClass Object
(
[zip_codes] => Array
(
[0] => stdClass Object
(
[zip_code] => 10015
[distance] => 0.521
)
[1] => stdClass Object
(
[zip_code] => 10079
[distance] => 0.521
)
[2] => stdClass Object
(
[zip_code] => 10094
[distance] => 0.521
)
I want only zip_code into an array, Please help me how do I get only zip_code into an array.
Thanks
So use array_map():
$result = array_map(function($x)
{
return $x->zip_code
}, $obj->zip_codes);
Try something like :
foreach($data->zip_codes as $zipObj)
{
echo $zipObj->zip_code;
}
This will loop over the zip codes array and output the relevant value.
Try something like this:
First iterate trought the collection of objects an get the zip code property and add it to an array
<?php
$result = array();
$zipCodes = $data->zip_codes;
for($i = 0; $i < sizeOf($zipCodes); $i++){
$result[] = $zipCodes->zip_code;
}
?>
You can use array_map, this function allows you to apply a callback to the array.
$zip_codes = array_map(function($i) { return $i->zip_code; }, $data->zip_codes);
I juste want to know how to read the value "status" in this PHP array:
Array
(
[0] => stdClass Object
(
[smsId] => 10124
[numberFrom] => +000
[numberTo] => +000
[status] => waiting
[date] => 20100825184048
[message] => ACK/
[text] => Test
)
[1] => stdClass Object
(
[smsId] => 10125
[numberFrom] => +000
[numberTo] => +000
[status] => waiting
[date] => 20100825184049
[message] => ACK/
[text] => Test 2
)
)
Thanks
Basically you have an array of objects. So you have to use a combination of array and object syntax to get your value:
$array[0]->status;
this breaks down into:
$object = $array[0]; // Array Syntax
$status = $object->status; // Object Syntax
$status = $array[0]->status; // Combined Array & Object Syntax
If you need to access each status in a loop, you do something like:
foreach($array as $obj){
$status = $obj->status;
}
$arr is the array you've got there.
for($i=0; $i<count($arr); $i++){
echo $arr[$i]->status;
}
Please take a look in the PHP Manual
You can do:
foreach($array as $arr) {
echo $arr->status;
}
If array variable is $arr
$arr[0]->status;
$arr[1]->status;