This is my code,
foreach ( $tick_data as $tick_index => $tick_entries ) {
foreach ( $base_data as $baseproject ) {
if ( $baseproject['name'] == $tick_entries['project_name'] )
$projects_id_tickcamp[$baseproject['id']]['tick_id'] = $tick_entries['project_id'];
}
}
which gives multidimensional array:
Array
(
[1181543] => Array
(
[tick_id] => 445196
)
[1287834] => Array
(
[tick_id] => 437574
)
)
and in this code, after apply post message function, i am getting different array named message id created an sub array:
foreach ( $projects_id_tickcamp as $basecamp_project_id => $tick_id ) {
$post_message_id = $this->post_message( $basecamp_project_id );
$projects_id_tickcamp[$basecamp_project_id]['message_id'] = $post_message_id;
}
Output of this code:
Array
(
[1181543] => Array
(
[tick_id] => 445196
[message_id] => 5208908
)
[1287834] => Array
(
[tick_id] => 437574
[message_id] => 5208912
)
)
Problem is if i am getting more $baseproject['id'] in future then i have to post message on that $baseproject['id'] except on the whole array again. Please have a look on this question, any suggestion would be grateful to me.
Related
I have to access to all values stored in a big multidimensional array, here's an example of print_r the array:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
[Periodo] => Array
(
[#attributes] => Array
(
[FechaIngreso] => 1998-12-12
[FechaRetiro] => 2021-11-12
[FechaLiquidacionInicio] => 2021-05-01
[FechaLiquidacionFin] => 2021-05-30
[TiempoLaborado] => 10829
[FechaGen] => 2021-05-05
)
)
[Devengados] => Array
(
[Basico] => Array
(
[#attributes] => Array
(
[DiasTrabajados] => 30
[SueldoTrabajado] => 1258955.00
)
)
)
)
The thing I want to do is extract the values from that array, I have tried this way:
<?php
$cunenov = $array['Novedad']['#attributes']['CUNENov'];
but doesn't work..
Any suggests?. Thanks in advance.
I wanted you to see the "constructed" array and the outputs .. Stand alone, this php works .. Paste it into a stand alone php file and determine what you're doing differently to not achieve the same result.
Building out the array, and then print_r so you can validate it's the same structure as your array.
<?php
$test = Array();
$test['Novedad'] = array();
$test['Novedad']['#attributes'] = array();
$test['Novedad']['#attributes']['CUNENov'] = 4545454545;
print_r( $test );
Yields:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
)
Then we echo:
$cunenov = $test['Novedad']['#attributes']['CUNENov'];
echo "Value is $cunenov";
Yields
Value is 4545454545
So i have a function that checks for reference in my system which is like a pyramid, i want to display who you invited and who he or she invited too, im using this foreach loop to get all reference under a user id and save them in json. but im having problem listing or extrating the results using php.
here is my pup recursive function to find all children or an entered user.
<?php
include 'includes/connect.php';
$menu['Users'] =getChildren(2);
echo '<pre>'; print_r($menu); // check tree array
function getChildren($parent_id)
{
$db=new mysqli('localhost','root','P=3h?9)Do#R#w2NQ','tobo'); //db
$refs=$db->query("SELECT * FROM users WHERE ref=$parent_id");
$children = array();
$i = 0;
foreach ($refs as $key => $downline) {
$children[$i] = array();
$children[$i]['fname'] = $downline['fname'];
$children[$i]['downline'] =getChildren($downline['id']);
$i++;
}
return $children;
}
$myJson = json_encode($menu);
$newJson = json_decode($myJson, true); ?>
it works fine and i get the following result:
Array
(
[Users] => Array
(
[0] => Array
(
[fname] => Andrey
[downline] => Array
(
[0] => Array
(
[fname] => Alisa
[downline] => Array
(
[0] => Array
(
[fname] => Maxim
[downline] => Array
(
[0] => Array
(
[fname] => james
[downline] => Array
(
)
)
)
)
)
)
)
)
[1] => Array
(
[fname] => Satori
[downline] => Array
(
)
)
)
)
my question is how can i get this result in a list using php so i can display the to the user as a list.
i tried to decode but im new to json so its still not usable by me. i hope you will be patient with my question structure, thank u very much
I have been trying to work this out for two days now and am hitting a brick wall. I have a skyscanner array that has flight itineraries where I have the flight
Leg - being Itineraries -> OutboundLegId -
and also the legs which shows the flight number - being
Legs -> FlightNumbers -> FlightNumber.
What I am trying to achieve is to display the Itinerary and then join the Flight Number on to that. But no matter what I try I cannot get this to work. I have read all about Keys on here and tried loads of examples but am coming up with nothing. Can someone point me in the right direction please?
Example arrays below
[Itineraries] => Array
(
[0] => Array
(
[OutboundLegId] => 13542-1610140610-29-0-13445-1610141240
[InboundLegId] => 13445-1610211340-29-0-13542-1610211640
[PricingOptions] => Array
(
[0] => Array
(
[Agents] => Array
(
[0] => 2174187
)
[QuoteAgeInMinutes] => 31
[Price] => 200.98
[DeeplinkUrl] => http://partners.api.skyscanner.net/apiservices/deeplink/v2?_cje=5JlLCgyPUKY0hT8T0Ybh6dL0Xf0htAiHTFX7RU79eeI3XvrsxvEqP1QUJAoHiHRd&url=http%3a%2f%2fwww.apideeplink.com%2ftransport_deeplink%2f4.0%2fUK%2fen-gb%2fGBP%2fcook%2f2%2f13542.13445.2016-10-14%2c13445.13542.2016-10-21%2fair%2fairli%2fflights%3fitinerary%3dflight%7c-32294%7c1152%7c13542%7c2016-10-14T06%3a10%7c13445%7c2016-10-14T12%3a40%2cflight%7c-32294%7c1153%7c13445%7c2016-10-21T13%3a40%7c13542%7c2016-10-21T16%3a40%26carriers%3d-32294%26passengers%3d1%2c0%2c0%26channel%3ddataapi%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d200.98%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3dskyscanner_b2b%26request_id%3d3bc96bda-fd7c-403a-b841-2ccc3c26071d%26commercial_filters%3dfalse%26q_datetime_utc%3d2016-09-29T08%3a18%3a27
)
[Legs] => Array
(
[0] => Array
(
[Id] => 13542-1610140610-29-0-13445-1610141240
[SegmentIds] => Array
(
[0] => 1
)
[OriginStation] => 13542
[DestinationStation] => 13445
[Departure] => 2016-10-14T06:10:00
[Arrival] => 2016-10-14T12:40:00
[Duration] => 270
[JourneyMode] => Flight
[Stops] => Array
(
)
[Carriers] => Array
(
[0] => 105
)
[OperatingCarriers] => Array
(
[0] => 105
)
[Directionality] => Outbound
[FlightNumbers] => Array
(
[0] => Array
(
[FlightNumber] => 1152
[CarrierId] => 105
)
)
)
Assuming this is one big array and its called $data you can nest a couple of foreach loops.
I use foreach loops as I assume there are cases where this data structure get more complex than the one you show
foreach ( $data['Itineraries'] as $itin ) {
foreach ( $data['Legs'] as $legs) {
if ($legs['Id'] == $itin['OutboundLegId']) {
// we matched the itinerary with a leg
echo $legs['OutboundLegId'] . ' ' . $legs['FlightNumbers'][0]['FlightNumber'];
}
}
}
Use it as draft. Can't perform function without feedback.
Put proper arrays instead of {YOUR-ARRAY-WITH-LEGS} and {YOUR-ARRAY-WITH-ITINERARIES}
$sortedLegs = array_column('Id', {YOUR-ARRAY-WITH-LEGS});
$joinedArray = array_map(function($itinerary) use($sortedLegs){
if(array_key_exists($itinerary['OutboundLegId'],$sortedLegs)) {
$itinerary['legs'] = $sortedLegs[$itinerary['OutboundLegId']];
}
return $itinerary;
},{YOUR-ARRAY-WITH-ITINERARIES});
I need help :)
I've to code a script that, cycling through an array inside an array , delete an element if in XXX field there isn't value (is NULL ).
My array is:
Array (
[idCampaign] => 3
[idIT] => 322
[recipients] =>Array (
[0] => stdClass Object ( [name] => minnie [email] => blabla#gmail.com [XXX] => )
[1] => stdClass Object ( [name] => [email] => fddd#gmail.it [XXX] => 0.88451100 )
) ) [date] => MongoDate Object ( [sec] => 1468503103 [usec] => 0 ) )
In this example the item [0] has no value in XXX value so my output array will be:
Array (
[idCampaign] => 3
[idIT] => 322
[recipients] =>Array (
[1] => stdClass Object ( [name] => [email] => fddd#gmail.it [XXX] => 0.88451100 )
) ) [date] => MongoDate Object ( [sec] => 1468503103 [usec] => 0 ) )
i hope that you can help me :)
You could use a nested foreach() Loop to cycle through the Data and then perform some tests, which on failing, guarantees that it is safe to unset the pertinent variable. Here's how:
<?php
// WE SIMULATE SOME DATA TO POPULATE THE ARRAY, ONLY FOR TESTING PURPOSES
$objDate = new stdClass();
$objRez1 = new stdClass();
$objRez2 = new stdClass();
$objRez1->name = "minnie";
$objRez1->email = "blabla#gmail.com";
$objRez1->XXX = null;
$objRez2->name = null;
$objRez2->email = "fddd#gmail.it";
$objRez2->XXX = 0.88451100;
$objDate->sec = 1468503103;
$objDate->usec = 0;
// IN THE END WE NOW HAVE A SAMPLE ARRAY (SIMULATED) TO WORK WITH.
$arrData = array(
'idCampaign' => 3,
'idIT' => 322,
'recipients' => array(
$objRez1,
$objRez2
),
'date' =>$objDate,
);
// LOOP THROUGH THE ARRAY OF DATA THAT YOU HAVE
// NOTICE THE &$data IN THE LOOP CONSTRUCT...
// THIS IS NECESSARY FOR REFERENCING WHEN WE UNSET VARIABLES WITHIN THE LOOP
foreach($arrData as $key=>&$data){
// SINCE THE XXX KEY IS STORED IN THE 'recipients' ARRAY,
// WE CHECK IF THE CURRENT KEY IS 'recipients' & THAT $data IS AN ARRAY
if($key == "recipients" && is_array($data)){
// NOW WE LOOP THROUGH THE DATA WHEREIN THE 'XXX' KEY LIVES
foreach($data as $obj){
// IF THE VALUE OF THE XXX KEY IS NULL OR NOT SET,
// WE SIMPLY UNSET IT...
if(!$obj->XXX){
unset($obj->XXX);
}
}
}
}
var_dump($arrData);
You can verify the Results HERE.
Hope this could offer you a little tip on how to implement it rightly on your own...
This should do the job
foreach($arrayOfObjects as $index => $object){
if(!isset($object->xxx) || empty($object->xxx)){
unset($arrayOfObjects[$index]);
}
}
I have multidimensional array which looks like this:
Array
(
[0] => Array
(
[banner_link] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] => 143038313801.jpg,143038313809.jpg,143038313811.jpg
)
[2] => Array
(
[banners] => 143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png
)
)
Now I want to rename banner_link key to banners.
Array
(
[0] => Array
(
[banners] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] => 143038313801.jpg,143038313809.jpg,143038313811.jpg
)
[2] => Array
(
[banners] => 143038306301.jpg,143038306302.jpg,143038306303.jpeg,143038306310.jpg,143038306311.jpg,143038306312.png
)
)
I have tried with bellow code but no luck.
foreach ( $getBefamousHomepage as $k=>$v )
{
$getBefamousHomepage[$k] ['banners'] = $getBefamousHomepage[$k] ['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
Getting this errors:
Notice: Undefined index: banner_link in /var/www/sitename/application/views/_templates/global_footer.php on line 14
Notice: Undefined index: banner_link in /var/www/sitename/application/views/_templates/global_footer.php on line 14
Array
(
[0] => Array
(
[banners] => http://www.eclipse-pos.com/eclipse/images/small_banner2.jpg
)
[1] => Array
(
[banners] =>
)
[2] => Array
(
[banners] =>
)
)
What I am doing wrong any idea?
Thanks.
banner_link doesn't always exist. Use isset() to see if it exists
:
if(isset($getBefamousHomepage[$k]['banner_link'])) {
$getBefamousHomepage[$k]['banners'] = $getBefamousHomepage[$k]['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
You should check if banner_link exists.
And to avoid overwriting banner you should check if banner not already exists.
foreach ( $getBefamousHomepage as $k=>$v ) {
if(isset($getBefamousHomepage[$k]['banner_link']) && !isset($getBefamousHomepage[$k]['banners'])) {
$getBefamousHomepage[$k]['banners'] = $getBefamousHomepage[$k]['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
}
Your code is almost good. You just need to add an extra check to see if banner_link exists.
When it does not, it throws a notice (like you see), but the code will still execute, so the "banner" index will be set to null.
Code example:
foreach ( $getBefamousHomepage as $k=>$v )
{
if (isset($getBefamousHomepage[$k]['banner_link']))
{
$getBefamousHomepage[$k] ['banners'] = $getBefamousHomepage[$k] ['banner_link'];
unset($getBefamousHomepage[$k]['banner_link']);
}
}
Some element of your array don't have banner_link and it is causing the warning when you try to access it. Try this instead:
if(isset($getBefamousHomepage[$k] ['banner_link'])) {
$getBefamousHomepage[$k] = array("banners" => $getBefamousHomepage[$k] ['banner_link']);
}