Replace the key in the array in php codeigniter - php

I have a string which i am spitting using explode functionality. Code Snippet is shown below
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key)
$invoicedata[] = (object)$key;
echo '<pre>'; print_r($invoicedata); exit;
The Output what i am getting right now is
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[scalar] => 2017167637
)
[1] => stdClass Object
(
[scalar] => 43
)
[2] => stdClass Object
(
[scalar] => 42
)
[3] => stdClass Object
(
[scalar] => 38
)
[4] => stdClass Object
(
[scalar] => 46
)
[5] => stdClass Object
(
[scalar] => 41
)
[6] => stdClass Object
(
[scalar] => 40
)
[7] => stdClass Object
(
[scalar] => 39
)
But the actual output what i need is in place of [scalar] i need [invoice]. How to do it? Any help appreciated.
Actual output what i need is shown below
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[invoice] => 2017167637
)
[1] => stdClass Object
(
[invoice] => 43
)
[2] => stdClass Object
(
[invoice] => 42
)
[3] => stdClass Object
(
[invoice] => 38
)
[4] => stdClass Object
(
[invoice] => 46
)
[5] => stdClass Object
(
[invoice] => 41
)
[6] => stdClass Object
(
[invoice] => 40
)
[7] => stdClass Object
(
[invoice] => 39
)
)
I tried with adding $invoice['invoice'] but it did not worked so how to get the desired output any help appreciated.

Here is working code. Try this.
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key=>$value)
{
$invoicedata[$key] = new stdClass();
$invoicedata[$key]->invoice = $value;
}
echo '<pre>'; print_r($invoicedata); exit;

Related

How can I concatenate a map structure to visualize it?

I am developing a game with maps (named channels) which are connected via warps. Each channel has an ID (int) and each warp has a channel source ID (int) and a channel target ID (int). A channel can have an infinite number of warps or no warps at all.
I built an administration app (with Zend Expressive / php and HTML) which gets the channel data from an API as JSON. This JSON is then parsed to simplified classes which include only the important data.
I tried to visualize the data in kind of a grid, to determine the border of the world.
I tried to visualize it with divs, with fixed size and ordering. Unfortunately I have no idea how to handle the variable number of neighbours. There are coordinates available where the warps are placed, but I want a more abstract view of the world. I just want every channel to be represented by a fixed size 2D cube (div with fixed width and height).
Channel class
<?php
namespace App\Entity;
class Channel
{
protected $id;
protected $neighbourIds = [];
public function parseData(array $data)
{
$this->setId($data['id']);
foreach($data['warps'] as $warp) {
$this->addNeighbourId($warp['targetChannelId']);
}
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
public function addNeighbourId($neighbourId)
{
$this->neighbourIds[] = $neighbourId;
}
/**
* #return mixed
*/
public function getNeighbourIds()
{
return $this->neighbourIds;
}
/**
* #param mixed $neighbours
*/
public function setNeighbours($neighbours): void
{
$this->neighbours = $neighbours;
}
}
Sample data in channels as print_r output
Array
(
[0] => App\Entity\Channel Object
(
[id:protected] => 1
[neighbourIds:protected] => Array
(
[0] => 2
[1] => 4
[2] => 5
[3] => 121
)
)
[1] => App\Entity\Channel Object
(
[id:protected] => 4
[neighbourIds:protected] => Array
(
[0] => 1
)
)
[2] => App\Entity\Channel Object
(
[id:protected] => 5
[neighbourIds:protected] => Array
(
[0] => 1
)
)
[3] => App\Entity\Channel Object
(
[id:protected] => 2
[neighbourIds:protected] => Array
(
[0] => 1
[1] => 3
[2] => 133
[3] => 138
[4] => 152
[5] => 175
[6] => 176
)
)
[4] => App\Entity\Channel Object
(
[id:protected] => 3
[neighbourIds:protected] => Array
(
[0] => 2
[1] => 126
[2] => 132
[3] => 137
)
)
[5] => App\Entity\Channel Object
(
[id:protected] => 121
[neighbourIds:protected] => Array
(
[0] => 1
[1] => 122
[2] => 139
[3] => 144
)
)
[6] => App\Entity\Channel Object
(
[id:protected] => 122
[neighbourIds:protected] => Array
(
[0] => 121
[1] => 123
[2] => 140
[3] => 145
)
)
[7] => App\Entity\Channel Object
(
[id:protected] => 123
[neighbourIds:protected] => Array
(
[0] => 122
[1] => 124
[2] => 141
[3] => 146
)
)
[8] => App\Entity\Channel Object
(
[id:protected] => 124
[neighbourIds:protected] => Array
(
[0] => 123
[1] => 125
[2] => 142
[3] => 147
)
)
[9] => App\Entity\Channel Object
(
[id:protected] => 125
[neighbourIds:protected] => Array
(
[0] => 124
[1] => 143
[2] => 148
)
)
[10] => App\Entity\Channel Object
(
[id:protected] => 126
[neighbourIds:protected] => Array
(
[0] => 3
[1] => 127
[2] => 131
[3] => 136
)
)
[11] => App\Entity\Channel Object
(
[id:protected] => 127
[neighbourIds:protected] => Array
(
[0] => 126
[1] => 128
[2] => 130
[3] => 135
)
)
[12] => App\Entity\Channel Object
(
[id:protected] => 128
[neighbourIds:protected] => Array
(
[0] => 127
[1] => 129
[2] => 134
)
)
[13] => App\Entity\Channel Object
(
[id:protected] => 129
[neighbourIds:protected] => Array
(
[0] => 130
[1] => 128
)
)
[14] => App\Entity\Channel Object
(
[id:protected] => 130
[neighbourIds:protected] => Array
(
[0] => 129
[1] => 131
[2] => 127
)
)
[15] => App\Entity\Channel Object
(
[id:protected] => 131
[neighbourIds:protected] => Array
(
[0] => 130
[1] => 132
[2] => 126
)
)
[16] => App\Entity\Channel Object
(
[id:protected] => 132
[neighbourIds:protected] => Array
(
[0] => 131
[1] => 133
[2] => 3
)
)
[17] => App\Entity\Channel Object
(
[id:protected] => 133
[neighbourIds:protected] => Array
(
[0] => 132
[1] => 2
)
)
[18] => App\Entity\Channel Object
(
[id:protected] => 134
[neighbourIds:protected] => Array
(
[0] => 135
[1] => 128
)
)
[19] => App\Entity\Channel Object
(
[id:protected] => 135
[neighbourIds:protected] => Array
(
[0] => 134
[1] => 136
[2] => 127
)
)
[20] => App\Entity\Channel Object
(
[id:protected] => 136
[neighbourIds:protected] => Array
(
[0] => 135
[1] => 137
[2] => 126
)
)
[21] => App\Entity\Channel Object
(
[id:protected] => 137
[neighbourIds:protected] => Array
(
[0] => 136
[1] => 138
[2] => 3
)
)
[22] => App\Entity\Channel Object
(
[id:protected] => 138
[neighbourIds:protected] => Array
(
[0] => 137
[1] => 2
)
)
[23] => App\Entity\Channel Object
(
[id:protected] => 139
[neighbourIds:protected] => Array
(
[0] => 140
[1] => 121
)
)
[24] => App\Entity\Channel Object
(
[id:protected] => 140
[neighbourIds:protected] => Array
(
[0] => 139
[1] => 141
[2] => 122
)
)
[25] => App\Entity\Channel Object
(
[id:protected] => 141
[neighbourIds:protected] => Array
(
[0] => 140
[1] => 142
[2] => 123
)
)
[26] => App\Entity\Channel Object
(
[id:protected] => 142
[neighbourIds:protected] => Array
(
[0] => 141
[1] => 143
[2] => 124
)
)
[27] => App\Entity\Channel Object
(
[id:protected] => 143
[neighbourIds:protected] => Array
(
[0] => 142
[1] => 125
)
)
[28] => App\Entity\Channel Object
(
[id:protected] => 144
[neighbourIds:protected] => Array
(
[0] => 145
[1] => 121
)
)
[29] => App\Entity\Channel Object
(
[id:protected] => 145
[neighbourIds:protected] => Array
(
[0] => 144
[1] => 146
[2] => 122
)
)
[30] => App\Entity\Channel Object
(
[id:protected] => 146
[neighbourIds:protected] => Array
(
[0] => 145
[1] => 147
[2] => 123
)
)
[31] => App\Entity\Channel Object
(
[id:protected] => 147
[neighbourIds:protected] => Array
(
[0] => 146
[1] => 148
[2] => 124
)
)
[32] => App\Entity\Channel Object
(
[id:protected] => 148
[neighbourIds:protected] => Array
(
[0] => 147
[1] => 125
)
)
[33] => App\Entity\Channel Object
(
[id:protected] => 152
[neighbourIds:protected] => Array
(
[0] => 2
)
)
[34] => App\Entity\Channel Object
(
[id:protected] => 175
[neighbourIds:protected] => Array
(
[0] => 2
)
)
[35] => App\Entity\Channel Object
(
[id:protected] => 176
[neighbourIds:protected] => Array
(
[0] => 2
)
)
)
I expect kind of a grid to visualize the channels in relation, to enable administration of the data (delete, extend, etc.). My question only refers to the visualization part.

How to loop collection object in laravel?

I am trying to foreach loop in laravel.Here is my print_r result of jobseekers.
Illuminate\Support\Collection Object (
[items:protected] => Array (
[0] => stdClass Object ( [myid] => 37 [max(call_back_date)] => 2018-12-12 )
[1] => stdClass Object ( [myid] => 60 [max(call_back_date)] => 2017-12-12 )
[2] => stdClass Object ( [myid] => 61 [max(call_back_date)] => 2017-11-30 )
[3] => stdClass Object ( [myid] => 73 [max(call_back_date)] => 2017-11-29 )
[4] => stdClass Object ( [myid] => 62 [max(call_back_date)] => 2017-11-28 )
[5] => stdClass Object ( [myid] => 63 [max(call_back_date)] => 2017-11-22 )
[6] => stdClass Object ( [myid] => 64 [max(call_back_date)] => 2017-11-15 )
[7] => stdClass Object ( [myid] => 66 [max(call_back_date)] => 2017-11-15 )
[8] => stdClass Object ( [myid] => 65 [max(call_back_date)] => 2017-11-10 )
[9] => stdClass Object ( [myid] => 54 [max(call_back_date)] => 2017-10-24 )
[10] => stdClass Object ( [myid] => 53 [max(call_back_date)] => 2017-09-30 )
[11] => stdClass Object ( [myid] => 48 [max(call_back_date)] => 2017-09-20 )
[12] => stdClass Object ( [myid] => 46 [max(call_back_date)] => 2017-08-26 )
[13] => stdClass Object ( [myid] => 58 [max(call_back_date)] => 2017-12-12 )
[14] => stdClass Object ( [myid] => 70 [max(call_back_date)] => 2017-11-29 )
[15] => stdClass Object ( [myid] => 47 [max(call_back_date)] => 2017-08-20 )
[16] => stdClass Object ( [myid] => 44 [max(call_back_date)] => 2017-08-18 )
[17] => stdClass Object ( [myid] => 72 [max(call_back_date)] => 2018-02-17 )
[18] => stdClass Object ( [myid] => 55 [max(call_back_date)] => 2000-12-12 )
[19] => stdClass Object ( [myid] => 49 [max(call_back_date)] => )
[20] => stdClass Object ( [myid] => 50 [max(call_back_date)] => )
[21] => stdClass Object ( [myid] => 74 [max(call_back_date)] => )
[22] => stdClass Object ( [myid] => 32 [max(call_back_date)] => )
)
)
and i tried like this
foreach ($jobseekers as $value) {
echo $value->myid;
}
it shows me only the first item. like "37", i expect many items. like "37,60,61 etc...".
i think you need to do more one loop to iterate the data.
update
try one of this
foreach($jobseekers ->items as value){}
or this
foreach ($jobseekers as $value) {
foreach ($value as $item) {}
}

Issue in looping stdClass Object in Php

I am facing strange issue in looping json object using php. Here is the format of my data
stdClass Object
(
[userform] => Array
(
[0] => stdClass Object
(
[id] => 69
[product] => testuser
)
[1] => stdClass Object
(
[id] => 70
[product] => testuser
)
[2] => stdClass Object
(
[id] => 71
[product] => testuser
)
[3] => stdClass Object
(
[id] => 72
[product] => testuser
)
[4] => stdClass Object
(
[id] => 73
[product] => testuser
)
[5] => stdClass Object
(
[id] => 74
[product] => testuser
)
[6] => stdClass Object
(
[id] => 75
[product] => testuser
)
[7] => stdClass Object
(
[id] => 76
[product] => testuser
)
[8] => stdClass Object
(
[id] => 77
[product] => testuser
)
[9] => stdClass Object
(
[id] => 78
[product] => testuser
)
[10] => stdClass Object
(
[id] => 79
[product] => testuser
)
[11] => stdClass Object
(
[id] => 80
[product] => testuser
)
[12] => stdClass Object
(
[id] => 81
[product] => testuser
)
[13] => stdClass Object
(
[id] => 82
[product] => testuser
)
[14] => stdClass Object
(
[id] => 83
[product] => testuser
)
[15] => stdClass Object
(
[id] => 84
[product] => testuser
)
[16] => stdClass Object
(
[id] => 85
[product] => testuser
)
[17] => stdClass Object
(
[id] => 86
[product] => testuser
)
[18] => stdClass Object
(
[id] => 87
[product] => testuser
)
[19] => stdClass Object
(
[id] => 88
[product] => testuser
)
[20] => stdClass Object
(
[id] => 89
[product] => testuser
)
)
)
But this loop throws following error.
Trying to get property of non-object in
Please let me know how to read the product property value from the above format
Thanks all
That foreach should be on $data->userform , not just $data
foreach($data->userform as $key=>$row){
if(isset($row->product)){
print_r($key);
print_r($row->product);
}else{
//product property is not set
}
}

Multidimensional array, find item and move to the top?

I'm trying to do some kind of function that will find (in the following array) the object with the id of 2, and move it to the top of the array. Here's the original array:
Array
(
[0] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[1] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
And here is what I'd like it to output (with the moved array item):
Array
(
[0] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[1] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
Any help would be appreciated.
function customShift($array, $id){
foreach($array as $key => $val){ // loop all elements
if($val->id == $id){ // check for id $id
unset($array[$key]); // unset the $array with id $id
array_unshift($array, $val); // unshift the array with $val to push in the beginning of array
return $array; // return new $array
}
}
}
print_r(customShift($data, 2));

stdClass Object foreach PHP

Been searching for a solution for my problem. Seams meny has the same q as me, but still haven't got a solution for my problem.
I have a stdClass Object that needs to be printed out in a foreach or somlike like that.
Here is a cut of the result i get with a "print_r($result)".
stdClass Object
(
[ServiceGroup] => Array
(
[0] => stdClass Object
(
[Service] => Array
(
[0] => stdClass Object
(
[_] => 3D-modulering
[count] => 71
)
[1] => stdClass Object
(
[_] => CAD
[count] => 81
)
[2] => stdClass Object
(
[_] => Databasutveckling
[count] => 118
)
[3] => stdClass Object
(
[_] => Datainmatare
[count] => 6
)
[4] => stdClass Object
(
[_] => Driftsteknik
[count] => 87
)
[5] => stdClass Object
(
[_] => IT-konsult
[count] => 39
)
[6] => stdClass Object
(
[_] => IT-tekniker
[count] => 223
)
[7] => stdClass Object
(
[_] => Mjuk- och hårdvarutestning
[count] => 150
)
[8] => stdClass Object
(
[_] => Nätverksteknik
[count] => 142
)
[9] => stdClass Object
(
[_] => Produkttestare
[count] => 171
)
[10] => stdClass Object
(
[_] => Programmerare
[count] => 146
)
[11] => stdClass Object
(
[_] => Projektledning
[count] => 156
)
[12] => stdClass Object
(
[_] => Serviceteknik
[count] => 157
)
[13] => stdClass Object
(
[_] => Support
[count] => 360
)
[14] => stdClass Object
(
[_] => Systemadministration
[count] => 145
)
[15] => stdClass Object
(
[_] => Systemutveckling
[count] => 110
)
[16] => stdClass Object
(
[_] => Webbdesign
[count] => 246
)
[17] => stdClass Object
(
[_] => Webbutveckling
[count] => 135
)
[18] => stdClass Object
(
[_] => webmaster
[count] => 103
)
)
[name] => Data/IT
)
[1] => stdClass Object
(
[Service] => Array
(
[0] => stdClass Object
(
[_] => Affärsanalys
[count] => 192
)
[1] => stdClass Object
(
[_] => Aktuarie
[count] => 20
)
I don't know exactly what you want, but can easily foreach through objects just like arrays. This example shows that.
foreach($result->ServiceGroup as $value){
foreach($value->Service as $obj){
echo $obj->_;
echo $obj->count;
}
echo $value->name;
}

Categories