I'd like ask on my problem with formatting array. I'm importing data to system from .XLSX. Function return me array which one looks like:
Array Index
My question is how I can insert this data to table if I know array with index 1 and with number 1 is Name e.t.c?
I can write SQL but I can't write foreach. I already tried make some foreach but without success.
Thank you one more time
Here is solution:
/** #var int $count */
$count = count($spreadsheet);
$s = array();
for($i = 1; $i < $count; ++$i) {
$s = $spreadsheet[$i];
}
return $s;
Related
I use the array_rand PHP function with an array. I use it with a data fixture function wich load a set of data in a loop like this:
$random_values = array();
for ($i = 0; $i < 20; $i++) {
$random_values[] = array_rand(["1","2","3","4","5"]);
}
My result is quite always "1" in the $random_values array, the native
PHP function seems not really random, Is there another stuff to do to
improve the randomization of my algorithm ?
Notice I already know there is an official documentation here, http://php.net/manual/fr/function.array-rand.php.
How's it going? So, with array_rand, it actually returns a random key within an array. Your current code does not put the random key value into the array you want to randomize. ie echo $random_value[$random_key]... See example below, hope this helps
$random_values = array("1","2","3","4","5");
for ($i = 0; $i < 20; $i++) {
$key = array_rand($random_values);
echo $random_values[$key] . "\n";
}
How can I loop through addAttribute to write multiple values.
So far it looks like this:
for($i = 0; $i < 10; $i++)
{
$this->title->addAttribute('names', "'".$this->Data[$i]->names.';');
}
I get this error:
SimpleXMLElement::addAttribute(): Attribute already exists
current the xml looks like this(without the loop, with a static value name):
<button names=Tim;"/>
but I want it to look like this after the loop:
<button names=Tim;Tom;Ted"/>
how do I achieve this?
you can achieve this by first create an array with all your name, and then implode your array in your attribute
This should work
PHP
$names = [];
foreach($this->Data as $key => $value) {
$names[] = $value->names;
}
$this->title->addAttribute('names', implode(';', $names));
You do not need to use addAttribute() to add value to exist attribute. Only add new value to attribute like bottom code
for($i = 0; $i < 10; $i++)
{
$this->title['names'] .= $this->Data[$i]->names.';'
}
Check result in demo
In your code sample, you'r trying to add same attribute. Instead of it, you can create an array with names. Then you can join array elements with a glue string with implode.
$names = [];
for($i = 0; $i < 10; $i++)
{
$names[] = $this->Data[$i]->names;
}
$this->title->addAttribute('names', implode(';',$names));
I´m trying to insert arrays into other array, the problem is that when I call the array_push() method it overwrites the last one element of my array, then I just get an array with data of one array (the last one):
$users_data = [];
$resultSize = count($result);
$data = $result;
for ($i = 0; $i < $resultSize; $i++) {
$person = [
'nombre' => $result[$i]['nombre'],
'apellido' => $result[$i]['apellido'],
];
array_push($users_data, $person);
// $users_data = $person; I also have tried with this method.
};
I just receive one object with this:
Object {nombre: jane, apellido: doe}
What is going wrong?
It shud be like this,
$person['nombre'][$i] = $result[$i]['nombre'];
$person['apellido'][$i] = $result[$i]['apellido'];
^ you have missed this index.
Then no need of array_push(). you can directly assign persons to user_data
Or like this :
for ($i = 0; $i < $resultSize; $i++) {
$users_data['nombre'][] = $result[$i]['nombre'];
$users_data['apellido'][] = $result[$i]['apellido'];
};
I'm trying to remove an object from an array if one of his properties is null or empty, this is the code.
The array has been sorted using this function:
function sortArray($c1, $c2)
{
return ($c1->propertyToCheck < $c2->propertyToCheck);
}
In case it changes anything.
$myArray = array();
...
// Add values to the array here
...
usort($myArray,"sortArray");
for($i = 0; $i < count($myArray ); $i++)
{
if(empty($myArray[$i]->propertyToCheck))
{
unset($myArray[$i]);
// var_dump($myArray[$i]) returns NULL
}
}
echo json_encode($myArray);
// Returns the entire array, even with the values that shouldn't be there.
The code is inside a function but the array is created inside said function.
I'm using echo json_encode($myArray) to send the value back in AJAX, but the array sent is the entire array with every object inside it.
The count($myArray) is the "problem".
Once the unset() is "reached" there is one element less in the array and therefore the next call to count($myArray) will return n-1 of the previous iteration -> your loop doesn't get to the end of the array.
You have at least three choices (in ascending order of my preference)
a)
$maxIdx = count($myArray);
for($i = 0; $i < $maxIdx; $i++) {
b)
foreach( $myArray as $key=>$obj ) {
if(empty($obj->propertyToCheck)) {
unset($myArray[$key]);
c)
$myArray = array_filter(
$myArray,
function($e) {
return !empty($e->propertyToCheck);
}
);
(...and many more)
see also: http://docs.php.net/array_filter
I have an array and looping through it and pushing the data using for loop.
$test_data = [10,20,35,01,50,60,87,12,45,86,9,85];
Actually that's a sample data. But my data is a result similar to that which I get from PostgreSQL, a single column data.
Using the below function for $test_data.
for( $x=0; $x < 12; $x++ ) {
$chart_data['data1'] = $test_data[$x];
}
Result : {"data1":{"data": 85"}}
Here's the for loop which I use along the PostgreSQL result in the PHP.
for( $x=0; $x < pg_num_rows($query); $x++ ) {
$data['data1'] = pg_fetch_assoc($query);
}
Even here I get only the last row in the browser when I do - echo json_encode($data);
Something similar to the result what I've mentioned above.
First 9 rows are not getting inserted into the data[]. Only the last row is getting added to the array.
Please say me where I'm doing the mistake.
Thanks in advance.
Since arrays cannot have same key as index.
You need to rewrite it as ..
for( $x=0; $x < 12; $x++ ) {
$chart_data['data1'][] = $test_data[$x];
// ^^ <--- Add that.
}
As Loic suggested go with a foreach , I answered quickly so it didn't strike on my mind in the first place
foreach($test_data as $val)
{
$chart_data['data1'][] = $val;
}
You could do like this (without using pg_num_rows):
// better initialize the result array
$data = array('data1' => array());
while ($row = pg_fetch_assoc($query)) {
// here is the point, add element to the array
$data['data1'][] = $row;
}