Tags down


php array_push empty

By : Roger Gutiérrez
Date : May 05 2020, 08:46 AM

I'm need to push data to an array inside a foreach loop. Empty values get pushed into the array.

I 'm trying logging out the values to see if they were empty but they were there, they only go missing when pushing to the array. I also tried to just assign the value to another variable and that worked fine.

$winners = \App\Winner::where('gameid', 577)->pluck('prizes_web_1'); $xml = simplexml_load_string(stripslashes($winners)); $winners_1 = []; foreach($xml->Winner as $v) { $out->writeln($v); //when logging here every value gets logged correctly array_push($winners_1, $v); } $out->writeln($winners_1); //here an array with 4 empty values gets logged

I trying to declaring the array as array(), the issue stayed. Tried assigning the value like so winners_1[] = $v still everything remained the same. Also tried using strval($v) but that didn't help either

I can not  figure out what is causing the problem, never have I come across something like that when pushing to arrays.

EDIT Here's code example of xml:

<?xml version='1.0' encoding='UTF-8'?>
    <Winner><name>Robb Stark</name></Winner>
    <Winner><name>Jon Snow</name></Winner>
    <Winner><name>Aria Stark</name></Winner>
    <Winner><name>Theon Greyjoy</name></Winner>

Also the $log->writeln() line are logging things to the console, when loging the $v I can see the values Robb Stark, Jon Snow etc logged, they dissapear when pushing to the array.

I just need to cast my xml name element and it worked. $winners_1[] = (string)$v->name; Answer :

The problem here wasn’t really adding the elements into the array, but what was actually added - and how it got processed / interpreted later on.

SimpleXML is what you’d traditionally call a “fickle mistress”. Often SimpleXMLElement instances behave like strings in certain contexts - but then don’t in a slightly different one.

I did not  go look up the inner workings of Symfony’s ConsoleOutput, but how exactly that creates output from the input objects has probably played a role here.

Casper is advice to cast them into strings was a good idea - if you don’t need any properties / methods a SimpleXMLElement object offers later on any more, and you just need their contained “data” - then casting them as soon as possible is a best way to avoid further troubles.

You can't  directly cast $v into a string here though - because the Winner element did not contain the text directly, but it was wrapped in an additional name element. Casting a SimpleXMLElement that in turn contains other elements into a string would just result in an empty string again.

So the name element itself needs to be accessed and cast into a string here - (string) $v->name

Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • insert line after paragraph word
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org