Manipulating arrays in php - php

I have an file uploading site, it has an option of uploading through urls, what I am trying to do is whenever a user uploads through url, I check my database if a file exists that was uploaded through same url it displays the download url directly instead of uploading it again.
The data sent to uploading script is in array form like:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
[1] => http://i41.tinypic.com/28cfub7.jpg
[2] => http://i41.tinypic.com/12dsa32.jpg
)
and the array used for outputing the results is in form like this:
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
[1] => Array
(
[id] => 44
[name] => 28cfub7.jpg
[size] => 105544
[descr] =>
[password] =>
[delete_id] => D392
[upload_id] => 6676FD881
)
[2] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Now I want is that if the link http://i41.tinypic.com/28cfub7.jpg is already upload I just add it to output array but maintain it in a order (if the link added was 2nd in array the output result should also show it in 2nd)
So what function should be used to remove the matched urls from input array and a function to add it output array in the order no.
// edited
Yes unset will do the thing but I want to maintain the order:
For example after unsetting the array looks like this:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
// [1] was removed
[2] => http://i41.tinypic.com/12dsa32.jpg
)
but the output array would be
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
// this will become [1], so how can i add another output[1] and shift other
// items after it to [2], [3] and so on...
[1] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)

Well, you can add it to the output array by doing something like:
$OutArray[2] = $element;
Where $element is another Array with the id, name, size (etc...) elements.
As for removing from the array:
unset($OutArray[2]);
You may want to read Array (PHP manual).

If you have an indexed array, you can remove a value by doing:
unset ($array[2]);
If you want to add an item to an array, use this shorthand of array_push (you don't need to specify an index!):
$array[] = "new object";
All documentation is on php.net/arrays

Why don't use an if statement and/or file_exists() to see if the file is there. If you already have an array with the values then it just won't be uploaded again.

Related

Getting out the value of a key in multidimentional array in php

In my php query I got this output:
{"projects":[{"id":127,"name":"efrat","status":{"id":10,"name":"development","label":"development"},"description":"","enabled":true,"view_state":{"id":10,"name":"public","label":"public"},"access_level":{"id":90,"name":"administrator","label":"administrator"},"custom_fields":[{"id":1,"name":"Customer email","type":"email","default_value":"","possible_values":"","valid_regexp":"","length_min":0,"length_max":50,"access_level_r":{"id":10,"name":"viewer","label":"viewer"},"access_level_rw":{"id":10,"name":"viewer","label":"viewer"},"display_report":true,"display_update":true,"display_resolved":true,"display_closed":true,"require_report":false,"require_update":false,"require_resolved":false,"require_closed":false}],"versions":[],"categories":[{"id":93,"name":"Monitor","project":{"id":0,"name":null}},{"id":31,"name":"Proactive","project":{"id":0,"name":null}},{"id":30,"name":"Project","project":{"id":0,"name":null}},{"id":29,"name":"Support","project":{"id":0,"name":null}}]}]}
after using 'json_decode' method on it, I get this:
"(
[projects] => Array
(
[0] => Array
(
[id] => 127
[name] => myprojectname
[status] => Array
(
[id] => 10
[name] => development
[label] => development
)
[description] =>
[enabled] => 1
[view_state] => Array
(
[id] => 10
[name] => public
[label] => public
)
[access_level] => Array
(
[id] => 90
[name] => administrator
[label] => administrator
)
[custom_fields] => Array
(
[0] => Array
(
[id] => 1
[name] => Customer email
[type] => email
[default_value] =>
[possible_values] =>
[valid_regexp] =>
[length_min] => 0
[length_max] => 50
[access_level_r] => Array
(
[id] => 10
[name] => viewer
[label] => viewer
)
[access_level_rw] => Array
(
[id] => 10
[name] => viewer
[label] => viewer
)
[display_report] => 1
[display_update] => 1
[display_resolved] => 1
[display_closed] => 1
[require_report] =>
[require_update] =>
[require_resolved] =>
[require_closed] =>
)
)
[versions] => Array
(
)
[categories] => Array
(
[0] => Array
(
[id] => 93
[name] => Monitor
[project] => Array
(
[id] => 0
[name] =>
)
)
[1] => Array
(
[id] => 31
[name] => Proactive
[project] => Array
(
[id] => 0
[name] =>
)
)
[2] => Array
(
[id] => 30
[name] => Project
[project] => Array
(
[id] => 0
[name] =>
)
)
[3] => Array
(
[id] => 29
[name] => Support
[project] => Array
(
[id] => 0
[name] =>
)
)
)
)
)
)"
In my PHP, how can I release the "name" object value (the result should be 'myprojectname') from this array? I've tried many foreach loops that got me nowhere.
Thank you,
It looks like you have one object, that when decoded actually only has one array item. So, in your case, ‘myprojectname’ may simply be “$projects[0][‘name’]”
If many array items, you could
foreach ($projects as $project) {
echo $project[‘name’];
}
EDIT: I took object provided and json_decoded it myself, it doesn't match the json_decoded item presented by OP -- the first image shows the code to var_dump 'name' OP desired, part of the code also below:
$decoded = json_decode($obj);
$projects = $decoded->projects;
$name = $projects[0]->name;
Your 'projects' contains an array ("projects":[{"id":127, ... }]). I assume that the 'projects'-array might contain multiple 'project'-objects like this?
{
"projects":
[
{
"id":127,
"name":"my-project"
},
{
"id":128,
"name":"my-other-project"
}
]
}
In that case you need the arrow notation to access the name property, for example:
foreach ($projects as $project_object) {
foreach ($project_object as $project) {
echo $project->name . '<br/>';
}
}
EDIT:
I took a minimal code example of the OP and got the expected result:
Can you add more details in your code snippets in your original question or provide us with a working example of your code?
There are some online PHP sandboxes that can help you with this. For example: I stripped out all code that does not seem related to your question and got the result you are looking for in two different ways:
http://sandbox.onlinephpfunctions.com/code/009c53671fd9545e4fcecfe4b0328974381ee2ce
It is also a good idea to sum up all the foreach loops that you already tried, so we can see if you were nearly there with your own solution. This way we can understand your question better and it prevents us from offering solutions that you already used.

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Select every first array value in associative array

I dynamically create this associative file array through a PHP script, and now I am wondering how do I select all values inside the root array?
I'll explain here in code.
This is the array that is currently getting generated everytime:
Array
(
[HTML] => Array
(
[0] => Index.php
)
[Javascript] => Array
(
[0] => Javascript.js
[1] => Jquery.js
)
[0] => New Text Document.txt
[Scripts] => Array
(
[0] => Get_Server_files.Script.php
)
[Style] => Array
(
[0] => General.css
[1] => Menu.css
[2] => Style.css
)
[images] => Array
(
[WelcomeImages] => Array
(
[0] => WelcomeImage0.png
[1] => WelcomeImage1.png
[2] => WelcomeImage2.png
[3] => WelcomeImage3.png
[4] => WelcomeImage4.png
[5] => WelcomeImage5.png
[6] => WelcomeImage6.png
)
[0] => bg.jpg
)
)
From here, how do I select every first value? As in selecting
HTML
Javascript
Scripts
Style
Images.
And not anything that is inside these arrays.
I have searched but everytime it's not quite what I would like.
You can use array_keys() to get the names and then use array_filter() to remove the 0 values:
$names = array_filter(array_keys($data));
print_r($names);
Output:
Array
(
[0] => HTML
[1] => Javascript
[3] => Scripts
[4] => Style
[5] => images
)
Demo.
Use array_keys()
$keys = array_keys($array);

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

Large data insertion

I have a csv file uploaded and data read into an array .The valid data is stored in the array
as associative array indexed from 0-4000 records
Array
(
[0] => Array
(
[uname] => uname1
[name] => fullname1
[email] => uname1#email.com
)
[1] => Array
(
[uname] => uname2
[name] => fullname2
[email] => uname2
)
[2] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
[3] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
..
...
[3999] => Array
(
[uname] => uname3
[name] => fullname3
[email] => uname3#email
)
)
How can i insert so many records because the $array is stored in variable when i click the submit the $array is reset to null.
How can I approach this without using database, any solution available?
Are you trying to say you're overflowing the 64KB limit of the <FORM> when you submit it? Make sure you're using method="POST" and encoding="multipart/form-data".
It's also possibly, but highly unlikely with 4000 records, that you're exceeding the post_max_size and upload_max_filesize parameters in your php.ini file. If you're secretly trying to post 50 million email addresses instead then that's likely your problem.
If that doesn't help, then you need to clarify what you're trying to accomplish, because we're left guessing. ;)

Categories